2012-07-06 151 views
1

我一直在嘗試做一個jQuery函數,它選擇所有連接到它的複選框。這是我一直在努力的代碼。我究竟做錯了什麼?jQuery複選框問題

<div id=checkboxes> 
<input type="checkbox" onclick="toggleChecked(this.checked)"> Select/Deselect All<br> 
<input type="checkbox" />Eggs<br> 
<input type="checkbox" />Butter<br> 
<input type="checkbox" />Milk<br> 
<input type="checkbox" />Bread<br> 
<input type="checkbox" />Jam<br> 
<input type="checkbox" />Tea<br> 
</div> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">     
     function toggleChecked(status) 
      $(document).ready(function(){ 
      $("#checkall").click(function(){ 
      var status = $(this).attr('checked'); 
      $('.checkbox').attr('checked',status); 
      }); 
      }); 
</script> 

P.S:我在網上找到了這段代碼。似乎爲其他人工作。

+1

你不能只抓碼斷網,並期望它與你的HTML工作。那個JS引用了你的html中不存在的元素:你沒有帶''checkall''的元素或帶有''複選框'''的元素。 – nnnnnn 2012-07-06 06:49:51

+0

我沒有將它與我的代碼合併。這些代碼都被分解到不同的模塊中。我把他們安排在一起。 – Shahzeb 2012-07-06 07:02:47

回答

-1

檢查這個link。它在這裏工作正常。

+0

是的。它在JSFiddle上運行良好。但是,當我將它複製到我的HTML並在Chrome上運行時,它突然停止工作。 :S – Shahzeb 2012-07-06 07:01:43

+0

你使用JSFiddle中的html嗎?並且它說鉻中的任何錯誤? – muthu 2012-07-06 07:08:09

+0

工作! :D原來我需要再次輸入

0

試試這個:

<div id=checkboxes> 
<input type="checkbox" id="checkall"> Select/Deselect All<br> 
<input type="checkbox" />Eggs<br> 
<input type="checkbox" />Butter<br> 
<input type="checkbox" />Milk<br> 
<input type="checkbox" />Bread<br> 
<input type="checkbox" />Jam<br> 
<input type="checkbox" />Tea<br> 
</div> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">     
      $(document).ready(function(){ 
       $("#checkall").click(function(){ 
       var status = $(this).attr('checked'); 
       $('input[type="checkbox"]').attr('checked',status); 
       }); 
      }); 
</script> 

這是Click事件處理程序結合的「負荷」的#checkall元素這是你想要什麼,並採取的JavaScript的onclick()方法關閉元素。此外,您還需要將ID和ID添加到Select all元素以供選擇器工作。

1

我認爲ür短缺的大括號在調用函數...

function toggleChecked(status) 
    { 
     $(document).ready(function(){ 
     $("#checkall").click(function(){ 
     var status = $(this).attr('checked'); 
     $('.checkbox').attr('checked',status); 
     }); 
     }); 
    } 
2

有很多問題。您試圖根據類選擇器設置複選框,但沒有任何複選框具有該類。此外,document.ready函數在您的事件處理程序中,應該是相反的方法... 使用prop()方法而不是attr()。並且正確安裝的情況下,是這樣的:

<div id=checkboxes> 
<input id="checkall" type="checkbox"> Select/Deselect All<br> 
<input class="checkbox" type="checkbox" />Eggs<br> 
<input class="checkbox" type="checkbox" />Butter<br> 
<input class="checkbox" type="checkbox" />Milk<br> 
<input class="checkbox" type="checkbox" />Bread<br> 
<input class="checkbox" type="checkbox" />Jam<br> 
<input class="checkbox" type="checkbox" />Tea<br> 
</div> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">     

    $(document).ready(function(){ 
     $("#checkall").on("click", function(){ 
      var status = $(this).is(":checked"); 
      $('.checkbox').prop('checked', status); 
     }); 
    }); 

</script> 
0

或者,你可以這樣做:

更改此

<input type="checkbox" /> 

<input type="checkbox" class="checkbox"/> 
0

出於某種原因,屬性值"true"和布爾型true對於複選框是不同的。同樣"false"和布爾false是不同的。

這是fiddle其中描述,嘗試更改布爾false"false",你會明白。

在你的問題中,確保你在狀態變量中接收到一個布爾值。

1

刪除onclick並使其onchange