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

标签 javascript jquery html

我正在尝试做什么

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

某些任务会有日/月 - 写为日/月数字。

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

计划

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

我的代码

这是该函数的代码(大量注释):

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>

最佳答案

在迭代单词时,您再次检查 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
    }
});

关于javascript - 找不到输入字符串中写入的特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30300461/

相关文章:

javascript - 两个按钮显示相同的 div。一键查看更多选项

javascript - 谷歌地图 - 变更中心

javascript - 当绘图管理器对标记处于事件状态时,自动单击 map 中心

javascript - 将 MS Word 粘贴为文本但保留一些样式 Jquery

jquery - 滚动条 : Output looks different although I use the same code as the example

javascript - 如何仅将错误消息从模型传递到 Controller (Mongoose)?

javascript - JavaScript 运行时如何将 BINARY( double 浮点格式)转换回 DECIMAL

php - Woocommerce 结帐页面中的承运商自定义字段验证

php - Jquery - 根据数组结果更改输入背景 css

javascript - CSS overlay div 显示不正确