检查所有复选框jquery [英] checking all checkboxes jquery

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

问题描述

为什么在我的js代码只需点击 name:check_all ,选中所有复选框即可。



strong> HTML:

 < div id =ss> 
< input type =checkboxname =check_all>
< / div>
< input type =checkboxname =checkedclass =idRow>
< input type =checkboxname =checkedclass =idRow>
< input type =checkboxname =checkedclass =idRow>
< input type =checkboxname =checkedclass =idRow>
< input type =checkboxname =checkedclass =idRow>
< input type =checkboxname =checkedclass =idRow>
< input type =checkboxname =checkedclass =idRow>
< input type =checkboxname =checkedclass =idRow>



jQuery

  $(document).on('click change','input [name =check_all]',function(){
var checkboxes = $('。 idRow');
if($(this).is(':checked')){
checkboxes.attr(checked,true);
} else {
checkboxes.attr(checked,false);
}
});

DEMO: http://jsfiddle.net/KdaZr/



我的jQuery版本是1.9。



如何解决?

解决方案

使用 方法。当你的标记被渲染的属性成为元素的属性,使用JavaScript你应该修改元素的属性。

  $(document) .on('change','input [name =check_all]',function(){
$('。idRow')。prop(checked,this.checked);
} );

http:// jsfiddle.net/GW64e/



请注意,如果 input [name =check_all] 没有动态生成没有必要委托事件。


Why in my js code can just with one click on name:check_all checking all checkboxes?

HTML:

<div id="ss">
    <input type="checkbox" name="check_all">
</div>
<input type="checkbox" name="checked" class="idRow">
<input type="checkbox" name="checked" class="idRow">
<input type="checkbox" name="checked" class="idRow">
<input type="checkbox" name="checked" class="idRow">
<input type="checkbox" name="checked" class="idRow">
<input type="checkbox" name="checked" class="idRow">
<input type="checkbox" name="checked" class="idRow">
<input type="checkbox" name="checked" class="idRow">

jQuery:

$(document).on('click change','input[name="check_all"]',function() {
    var checkboxes = $('.idRow');
    if($(this).is(':checked')) {
        checkboxes.attr("checked" , true);
    } else {
        checkboxes.attr ( "checked" , false );
    }
});

DEMO: http://jsfiddle.net/KdaZr/

My jQuery version is 1.9.

How can fix it?

解决方案

Use prop method. When your markup is rendered attributes become properties of the elements, using JavaScript you should modify properties of the element.

$(document).on('change','input[name="check_all"]',function() {
    $('.idRow').prop("checked" , this.checked);
});

http://jsfiddle.net/GW64e/

Note that if input[name="check_all"] is not generated dynamically there is no need to delegate the event.

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

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