2016-11-23 22 views
1

你好可以有人知道是否有可能這樣做?如何使用.find在jquery中正確獲取值

$(document).ready(function(){ 
     $('body').delegate('#check-hide-empty-table', 'click', function(){ 
      if ($(this).is(':checked')) { 
       alert("checked") 
       $('table').find('tr td.test').parents('tr').hide(); 
      } 
      else { 
       alert("uncheck") 
       $('table').find('tr td.test').parents('tr').show(); 
      } 
     }); 

和PHP文件中像這樣

</script> 
<table id="myTable" class="table table-responsive table-hover table-striped table-fixed tablesorter"> 
    <thead> 
     <th><input type="checkbox" value="hide" id="check-hide-empty-table"> Hide Empty</th> 
    </thead> 

    <tbody> 
    <?php 
     $first = false; 
     foreach ($data["data"] as $list) { 
      $isfull = $list["Players"] == $list["MaxUsers"] ? true : false; 
    ?> 

     <tr class="tabClicker" data-tab="tabs-1" id="<?=$list["Id"];?>"> 
      <td class= "test"><?= $list["Players"]."/".$list["MaxUsers"];?></td> 
     </tr> 
     <?php } ?> 
    </tbody> 
</table> 

我想要做的是,我想創建隱藏chechbox功能時$列表[ 「玩家」] = 0但我搞不清如何到,所以我試圖用取值爲.find(TR td.test)如果.find(TR td.test)= 0,該表將被隱藏

如何做到這一點?此代碼現在仍然是錯誤的。感謝您的任何幫助

回答

1

首先請注意,delegate()方法已被棄用。您應該使用on()。如果您使用的是1.7以上版本的jQuery,則需要升級它,理想情況下至少爲1.12。

在處理單選按鈕和複選框以提高可訪問性時,您還應該使用change事件。

解決您最實際的問題,你可以使用filter(),像這樣:

$('#myTable').on('change', '#check-hide-empty-table', function() { 
    $('table').find('tr td.test').filter(function() { 
     return $(this).text().trim() == "0"; 
    }).parents('tr').toggle(!this.checked); 
}); 

在這個例子中,我從checked屬性提供布爾值toggle()方法否定了if聲明的必要性。

Working example

+0

感謝的人對你的答案,但我有問題,因爲** <?= $名單[ 「玩家」。 「/」。$列表[ 「MAXUSERS」] ;?> **有2個值,結果像0/8或0/5不只是0.我只想再問一次,如果我這樣做是否有效? '<?= $ list [「Players」]?>/<?= $ list [「MaxUsers」];?>'now this code is work @Rory McCrossan – Pentolan

+0

Yep - 這非常好,我也會修復它。 –

相關問題