JavaScript If语句有效,但Else没有

时间:2014-05-08 20:47:56

标签: javascript jquery datatables multiple-select

当我运行此代码时,onClick下的if语句会在view.checked == true时计算,但是当view.checked == false时else不会计算。警报显示view.checked似乎正常工作。不知道发生了什么。我正在使用这两个插件:

带有fnFilter插件的

https://datatables.net/

http://wenzhixin.net.cn/p/multiple-select

作为旁注,还试图弄清楚为什么我的.join()方法会添加空格。

var checked = [];
var checkedreg = '';
$(document).ready(function() {
    rtable = $('#ropes').dataTable( {
        "ajax": "data/ropes.txt",
        "dom": 'C<"clear">lfrtip',
        "columns": [
            { "data": "brand" },
            { "data": "model" },
            { "data": "length" },
            { "data": "dia" },
            { "data": "price" },
            { "data": "drytreat" },
            { "data": "midmark" },
            { "data": "bipat" },
            { "data": "falls" },
        ],
    });
    $('select').multipleSelect({
       onClick: function(view) {
        checkedreg = ''; 
        alert(view.checked);
        if (view.checked) { //if checkbox is checked
                checked.push(view.label); //add value of checkbox to "checked" array (ex. "Petzl")
            } 
            else {
                checked.splice(array.indexOf(view.label), 1); //if checkbox is not checked, remove value from array
                alert(checked); //test
            }
            checkedreg = checked.join("|"); //create regex of values ("Petzl| Mammut| Edelweiss)"
            checkedreg = checkedreg.replace(/\s+/g, ''); //for some reason it adds spaces so i have to remove them
            alert(checkedreg); //test
            alert(checked); //test
            rtable.fnFilter(checkedreg, 0, true); //filters a column based on a regex and column index
        }
    })
});

1 个答案:

答案 0 :(得分:2)

checked.splice(array.indexOf(view.label), 1);替换为checked.splice(checked.indexOf(view.label), 1);并尝试