2013-05-04 74 views
1
<script> 
    $(function(){ 
    $('.parent').on('click',function(){ 
    $(this).find('.child[]').attr('checked',this.checked); 
    }); 
}); 
</script> 
<table> 
    <thead> 
    <tr> 
     <th>S No.</th> 
     <th><input type="checkbox" name="select1" id="select1" class="parent" /></th> 
     <th>Title</th> 
     <th>End Date</th> 
     <th>Action</th> 
     <th>Deal Type</th> 
     <th>Status</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
    <?php $sql="select * from table_name where id='$id'"; 
     $result=mysql_query($sql); 
     $sn=1; 
     while($show=mysql_fetch_array($result)) 
     { 
      $student=$show['student']; 

     ?> 
     <td><?php echo $sn; ?></td> 
     <td><?php echo "<input type='checkbox' name='check[]' class='child[]' id='check[]' value='".$student."'/>"; ?></td> 
     <td>enddate</td> 
     <td>View</td> 
     <td>Edit</td>            
     <td>Delete</td> 
     <td>xyz</td> 
    </tr> 
    <?php $sn++ }}?> 

這裏,checkbox-name=select1超出循環,它是父級複選框。 checkbox-name=check實際上是一個checkbox數組,隨數據庫一起提供數據。如果數據庫中有10個條目,則會有10個複選框。我想,如果我檢查父複選框,那麼所有的複選框應該檢查,不管你可以說的數字,就像gmail一樣。也請告訴在哪裏放置JavaScript/jQuery。謝謝您的回答。如何選擇/取消選中父複選框時在循環中生成的所有複選框

+0

我也將腳本標記移出了html表格。正確的格式可以幫助查找問題。我無法找到'while'循環的'}','tr'打開的標籤也出現在循環之外。 – Arjan 2013-05-04 07:31:58

回答

1

您點擊回調中的this會引用複選框本身。所以$(this).find('.child[]')不會找到任何元素。

另請注意,class="index[]"不適用於jQuery,因此您想將其更改爲class="index"。您可以保留name="index[]"原樣。

如果您想尋找父表的範圍內的元素(從而使你擁有的(未多種結構)檢查所有),你可以使用:

$(function(){ 
    $('.parent').on('click',function(){ 
     $(this).parents('table').find('.child').attr('checked',this.checked); 
    }); 
}); 

如果你想看看只是文檔中的所有子節點,你可以使用:

$(function(){ 
    $('.parent').on('click',function(){ 
     $('.child').attr('checked',this.checked); 
    }); 
}); 

親切的問候,

阿諾德·

+0

謝謝你阿諾德....你可以告訴我在哪裏放置這個jquery..Is它在連同其他javasercript代碼或在父母的checbox代碼........我一直讓代碼在,但仍然無法正常工作... – Student 2013-05-04 08:16:23

+0

這不是你現在擁有的代碼。 – Roonaan 2013-05-04 08:26:19

+0

對不起,先生,但仍然沒有工作....孩子複選框進入循環,但在jquery中,我們沒有提到,該孩子是一個數組。可以啓發我的概念,因爲我是新的jquery ....如果你可以我會很高興..感謝你... – Student 2013-05-04 08:36:49

相關問題