设置复选框,其中父类具有一定的CSS不确定状态



我在页面的某个地方有一个表,它的结构如下:

...<td class="ind"><input type="checkbox" .../> ... </td>
   <td><input type="checkbox" .../> ... </td> 
   <td><input type="checkbox" .../> ... </td> 

对于<td>中包含的每个复选框,该类具有ind,我想将该复选框设置为其不确定状态,如下所示

我怎样才能使这个部件工作呢?

$(document).ready(function() {
        var checkboxes = $(".ind").find(':checkbox');
        for (var i = 0; i < $(checkboxes).length; i++) {
            checkboxes[i].prop("indeterminate", true);
        }
    });

checkboxes[i]给你DOM节点,而不是jQuery对象,所以你不能在那里使用.prop。使用checkboxes[i].indeterminate = truecheckboxes.eq(i).prop('indeterminate', true)

当然代码可以简化。您不需要遍历复选框来设置属性,您可以立即将其设置为整个集合,jQuery将在内部处理迭代:

$('.ind :checkbox').prop('indeterminate', true);

这是一行字

$(".ind :checkbox").prop("indeterminate", true);

注意,jQuery总是对所有选定的元素起作用。你不需要写循环

。prop是一个jQuery方法,只是$.each():

$(document).ready(function() {
    var checkboxes = $(".ind").find(':checkbox');
    // for (var i = 0; i < $(checkboxes).length; i++) {
    //    checkboxes[i].prop("indeterminate", true);
    // }
    checkboxes.each(function() {
        $(this).prop("indeterminate", true);
    });
});

edit:正如tomlak所说,这实际上是一行:),因为你可以通过$(".ind").find(':checkbox')选择所有想要的复选框;你可以简单地继续做jQuery awesome:

$(".ind").find(':checkbox').prop("indeterminate", true);

相关内容

最新更新