2013-02-07 45 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>