选择使用jQuery的所有复选框 [英] Select All checkboxes using jQuery

查看:200
本文介绍了选择使用jQuery的所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有跟随着HTML code:

I have the followin html code:

    <input type="checkbox" id="ckbCheckAll" />
    <p id="checkBoxes">
        <input type="checkbox" class="checkBoxClass" id="Checkbox1" />
        <br />
        <input type="checkbox" class="checkBoxClass" id="Checkbox2" />
        <br />
        <input type="checkbox" class="checkBoxClass" id="Checkbox3" />
        <br />
        <input type="checkbox" class="checkBoxClass" id="Checkbox4" />
        <br />
        <input type="checkbox" class="checkBoxClass" id="Checkbox5" />
        <br />
    </p>

,当用户检查 ckbCheckAll 所有复选框必须检查。另外我有以下的jquery code:

which when user check ckbCheckAll all checkboxes must be checked. Also I have following jquery code:

    $(document).ready(function () {
        $("#ckbCheckAll").click(function () {
            $(".checkBoxClass").attr('checked', this.checked);
        });
    });

当我看到我的浏览器页面,我得到以下结果:
ckbCheckAll 第一次点击的所有复选框将检查(这是正确的)。在 ckbCheckAll 第二次点击所有的复选框取消勾选(这是正确的)。但在3TH尝试什么都不会发生!也是在第四尝试什么都不会发生,等等。

When i see my page on browser i get following result: In the first click on ckbCheckAll all checkboxes will check( which is correct). In the second click on ckbCheckAll all checkboxes will uncheck (which is correct). But in 3th attempt Nothing will happen! also in 4th attempt nothing will happen and so on.

问题出在哪里?

请原谅我和糟糕的英语。

Please excuse me for poor and bad English.

推荐答案

使用道具

$(".checkBoxClass").prop('checked', true);

或取消:

$(".checkBoxClass").prop('checked', false);

http://jsfiddle.net/sVQwA/

$("#ckbCheckAll").click(function () {
    $(".checkBoxClass").prop('checked', $(this).prop('checked'));
});

这篇关于选择使用jQuery的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆