找不到输入字符串中写入的特定字符

时间:2015-05-18 10:13:10

标签: javascript jquery html

我正在尝试做什么

我正在构建的应用是一个任务列表,显示在input文本字段中。

某些任务将有一天/月 - 写为日/月数字。

当应用程序刷新时,它会调用函数callBackTime(),该函数标识哪些行设置了日/月,然后将对该数据执行某些操作。

计划

该函数首先扫描所有input字段,查找/,如果找到,将查找写入的日期和月份。目前,我需要做的就是在控制台中输出这些值,但我无法做到这一点。

我的代码

这是函数的代码(评论很多):

function callBackTime(){

    //for each input
    $('div#rows>div.column>div.row>div.input-group>input.row-name').each(function(){
        var searchResult = $(this).val().toString(); //value of this input
        //if '/' is found ...
        if(searchResult.indexOf('/') >= 0) {

            //separate string to individual words
            var words = searchResult.split(' '); 

            words.each(function(){ //for each word...
                if(searchResult.indexOf('/')>=0){ //find the word that contains '/'
                    var dayMon = words.split('/'); //separate into day and month values
                    var day = dayMon[0]; //first in array is day (we're not American)
                    var mon = dayMon[1]; //second in array month
                    console.log(day+' of '+mon); //log the data
                }
            });
        }
    })
}

示例行可能如下所示:

<div class="container" id="rows">
    <div class="col-md-12 column">
        <div class="row" data-id="35" data-wr_replaced="true">
            <div class="input-group" data-wr_replaced="true">
                <div class="input-group-btn" data-wr_replaced="true">
                    <button type="button" class="btn btn-default task-delete" 
                            data-toggle="modal" data-target=".modal-delete" 
                            onclick="deleteRow(1,35)" data-wr_replaced="true">
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                    </button>
                </div>
                <input type="text" class="form-control row-name" 
                       value="Balmforth Associates - Bryony 12/5 (ad on Reed)">
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

在迭代单词时,再次检查searchResult /字符。我想你的意思是:

//separate string to individual words
var $words = $(searchResult.split(' '));

$words.each(function (index, word){ //for each word...
    if (word.indexOf('/') >= 0){ //find the word that contains '/'
        var dayMon = word.split('/'); //separate into day and month values
        var day = dayMon[0]; //first in array is day (we're not American)
        var mon = dayMon[1]; //second in array month
        console.log(day + ' of ' + mon); //log the data
    }
});
相关问题