CheckboxList - 从obj数组中按值勾选复选框

时间:2014-03-04 07:09:23

标签: jquery

cblPeriods是checboxlist控件的id。下面注释的行不起作用,我不知道为什么它不使用chkboxes对象。但是当我使用jquery选择器时,注释行下面的行工作正常。

请说明为什么我不能使用chkboxes obj。我从firebug附上了手表窗口的截图。声明

$(chkboxes[value=-1]).prop("checked", false);

不会产生任何对象......

  $(function () {
        $('#cblPeriods').find(":checkbox").on("click", function () {
            var $obj = $(this);
            var selVal = $obj.val();
            var chkboxes = $('#cblPeriods').find(":checkbox");

            if (selVal == '-1') { //No
                $(chkboxes).not($obj).removeAttr("checked");
            }
            else if (selVal == '0') { //All
                $(chkboxes).not($obj).prop('checked', true);
                //$(chkboxes).find(":checkbox[value=-1]").removeAttr("checked"); //THIS ONE IS NOT WORKING..
                $('#cblPeriods').find(":checkbox[value=-1]").removeAttr("checked");
            }

        });
    });

enter image description here

3 个答案:

答案 0 :(得分:2)

在JQuery中,find()会在DOM中找到与您的描述匹配的子项。注意

var chkboxes = $('#cblPeriods').find(":checkbox");

找到一个嵌套在id为cblPeriods的元素下的复选框。当您在注释行中使用$(chkboxes).find(":checkbox[value=-1]")时,您告诉JQuery找到嵌套在cblPeriods下嵌套的复选框下的复选框。显然,这不是你想要的。

也许用这个替换它:

$(chkboxes).filter(":checkbox[value=-1]").removeAttr("checked");

答案 1 :(得分:1)

这段代码可以用吗?

 var chkboxes = $('#cblPeriods');
 $(chkboxes).find(":checkbox[value=-1]").removeAttr("checked");

答案 2 :(得分:0)

你需要写,

$(chkboxes[value=-1]).removeAttr("checked"); //THIS ONE IS NOT WORKING..