1. 程式人生 > >JQuery實現全選全不選

JQuery實現全選全不選

$('input[name="selectall"]').click(function(){
    //input[name="selectall"]:實現全選與全部選的checkbox
    //alert(this.checked);
if($(this).is(':checked')){
        $('input[name="stuCheckBox"]').each(function(){
            //input[name="stuCheckBox"]:被選中的CheckBox
            //此處如果用attr,會出現第三次失效的情況
$(this).prop("checked"
,true); }); }else{ $('input[name="stuCheckBox"]').each(function(){ $(this).removeAttr("checked",false); }); //$(this).removeAttr("checked"); } });
<td><input type="checkbox" name="selectall"/></td>
<td><input type="checkbox" name="stuCheckBox" /></td
>