2013-02-07 44 views
4

我从stackoverflow得到这个代码,看起来像一个很不错的“全选”复选框解决方案,任何想法为什么它失败后第二次点击?选择所有复选框只能工作两次

http://jsfiddle.net/R9zjk/2/

<table> 
    <tr> 
     <td> 
      <input type='checkbox' value='0' class=''> 
     </td> 
     <td> 
      <input type='checkbox' value='0' class=''> 
     </td> 
     <td> 
      <input type='checkbox' value='0' class=''> 
     </td> 
     <td width="100" align="right"> 
      select all <input type='checkbox' value='0' class='selectall2'> 
     </td> 
    </tr> 
</table> 

$(document).ready(function() { 


    $(document).on("click", ".selectall2", function() { 


     $(this).closest('tr').find('input[type=checkbox]').attr('checked', this.checked); 


    }); 



}); 

回答

19

使用.prop()代替.attr()以上的jQuery 1.6

如果使用jQuery 1.6,代码if ($(elem).attr("checked"))将检索的实际内容属性,当复选框被选中,它不改变并未检查。它仅用于存储已检查属性的默认值或初始值。为了保持向后兼容性,jQuery 1.6.1+中的.attr()方法将为您检索并更新属性,因此不需要将布尔属性的代码更改为.prop()。尽管如此,获取选中值的首选方法是使用上面列出的选项之一。要查看最新的jQuery如何工作,请在下面的示例中选中/取消选中复选框。

.prop()

演示 - http://jsfiddle.net/f9QYx/

+0

AHHHH确定。谢谢 –

+0

新学习对我来说也是如此:) –

+0

非常感谢... –

0

您的代码不会对jQuery的1.9工作,是因为版本的jQuery框架,你有,选择1.8.3和它的工作。

Live Demo

$(document).ready(function() { 
    $(document).on("click", ".selectall2", function() { 
     $(this).closest('tr').find('input[type=checkbox]').attr('checked', this.checked); 
    }); 
}); 

您应该道具使用,而不是为ATTR jQuery 1.6 and above

Live Demo

$(document).ready(function() { 
    $(document).on("click", ".selectall2", function() { 
     $(this).closest('tr').find('input[type=checkbox]').prop('checked', this.checked); 
    }); 
}); 

从jQuery 1.6开始,对于尚未设置的属性 ,.attr()方法返回undefined。检索和修改DOM性能如 所检查的,所选择的,或禁用形式元素的状态,使用 .prop()方法,jQuery doc

0

尝试使用.attr('checked', 'checked')

$(document).ready(function() { 
    $(document).on("click", ".selectall2", function() { 
     $(this).closest('tr').find('input[type=checkbox]').attr('checked', 'checked'); 
    }); 
}); 
0

一些问题是解决见here

HTML代码的现场演示是:

<table> 
    <tr> 
     <td> 
      <input type='checkbox' value='0' class='abc'> 
     </td> 
     <td> 
      <input type='checkbox' value='0' class='abc'> 
     </td> 
     <td> 
      <input type='checkbox' value='0' class='abc'> 
     </td> 
     <td width="100" align="right"> 
      select all <input type='checkbox' value='0' class='selectall2'> 
     </td> 
    </tr> 
</table> 

和Javascript是:

$(document).ready(function() { 
    $(document).on("click", ".selectall2", function() { 
     $(this).closest('tr').find('input[type=checkbox]').attr('checked', this.checked); 
    }); 
    $(document).on("click",".abc",function(){ 
     var temp=0; 
     if(!$(".abc").attr('checked')) 
     { 
      temp=1; 
     } 
     if(temp==0) 
     { 
        $(".selectall2").attr('checked',true); 
     } 
     else 
     { 
        $(".selectall2").attr('checked',false); 
     } 
    }); 
}); 
-1

这会工作,短期和方便代码

<script> 
    $('#select_all').click(function() { 
    $(this).closest('table').find(':checkbox').prop('checked' , this.checked ? true : false); 
}) 
</script>