天天看點

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="stuCheckBox" /></td>