jQuerycheckbox全选/取消全选

用JavaScript使页面上的一组checkbox全选/取消全选,逻辑很简单,实现代码也没有太难的语法。但使用jQuery实现则更简单,代码也很简洁,精辟!

jQuery版本:1.3.2

Code

jQuery.attr  获取/设置对象的属性值,如:

$("input[name='chk_list']").attr("checked");     //读取所有name为'chk_list'对象的状态(是否选中)

$("input[name='chk_list']").attr("checked",true);      //设置所有name为'chk_list'对象的checked为true

再如:

$("#img_1").attr("src","test.jpg");    //设置ID为img_1的<img>src的值为'test.jpg'
$("#img_1").attr("src");     //读取ID为img_1的<img>src值

 下面的代码是获取上面实例中选中的checkbox的value值:

Code

 哪位高手能把上面遍历的过程用$.each()写出来,不胜感激。

在此谢谢rainnoless的帮助,下面是用$.each()遍历的代码:

<script type="text/javascript">
    
var arrChk=$("input[name='chk_list']:checked");
    $(arrChk).each(
function(){
       window.alert(
this.value);                        
    }
); 
});
</script>
原文地址:https://www.cnblogs.com/bynet/p/1602491.html