2011-03-23 47 views
0

我想獲取所選複選框的所有id值。當用戶點擊「selectAll」時,所有複選框都被選中,但我無法獲得他們的個人價值。任何想法我做錯了什麼?如何在selectAll被選中時選擇複選框的所有值?

這裏是jQuery的:

 $(".selectAll").unbind("click").click(function(e){//needs work here 
     var bool = $(this).is(":checked"); //gets whether selected or not 
     var list = new Array(); 

     $(function(){ 
      $("input:checkbox").attr("checked", bool); 

      if(bool == true){ 
      $.each((".delID :checked"),function(){ 
       list.push($(this).val()); 
       alert($(this).val()); 
      }); } 
     }); 
    }); //END of needs work area. 

這裏是HTML:

<form name="selectDelete" class="selectDelete"> 
<table id="inboxTable" class="txt13" width="800px"> 
<tr><th align="left" style="text-decoration:underline"><input type="checkbox" name="selectAll" class="selectAll"/></th> 
    <th style="text-decoration:underline">To</th><td width="20px"></td> 
    <th style="text-decoration:underline">Subject 
    </th><td width="20px"></td> 
    <th style="text-decoration:underline">Date</th><td width="20px"></td> 
    <th style="text-decoration:underline">Action</th></tr> 

<?php while($info=mysql_fetch_array($result)){ 
    $id = $info['id']; ?> 

    echo "<tr> 
      <td width="20px"><input type="checkbox" name="delID" class="delID" value="'.$id.'"/></td>"; 

只是爲了人在那裏有類似的問題,這是我落得這樣做使代碼工作:

   if(bool == true){ 
      $(".delID").each(function(index,element){ 
       //list.push($(this).val()); 
       alert(index + " " + $(this).val()); 
      }); } 

在我的情況下,如果'bool == true',那麼所有的複選框將總是被檢查,所以th at爲什麼只使用.delID而不是.delID:checked對我有用。 感謝所有的輸入。

回答

0

您可以使用此代碼:

$(".delID:checked").each(function(){ 
    list.push($(this).val()); 
    alert($(this).val()); 
}); 

http://jsfiddle.net/ynhat/daQSM/

+0

是的,我終於理解了它自己。感謝您的善意輸入。 – johnyboy 2011-03-23 06:18:44

相關問題