选中复选框以取消选中

时间:2016-12-15 10:03:44

标签: jquery

我无法选中复选框以取消选中复选框。

$("#search_list").find('.mselect input:checked').each(function(index, ele) {
            var prodId = $(this).val();
            console.log($(this));
            //if($(this).is(':checked')){
            //if($(this).length) {
                $(productList).each(function(key, value) {
                    console.log("ohh"+value);
                    if(prodId == value){
                        $(this).prop('checked', false);
                        console.log($(this));
                        console.log("unchedk"+value);
                    }
                });
            //}
        });

我的错误是什么?

1 个答案:

答案 0 :(得分:0)

上下文$(this)引用productList的当前元素/属性。将$(this)复选框分配给变量,例如(删除注释代码)

$("#search_list").find('.mselect input:checked').each(function(index, ele) {

    var $checkbox = $(this), // now use $checkbox instead of $(this) in the next loop 
                             // (the dollar sign is to indicate that the variable is alreay a jQuery object).
        prodId = $(this).val();

    $(productList).each(function(key, value) {

        if (prodId == value) {
            $checkbox.prop('checked', false);
        }

    });

});
相关问题