2013-04-10 82 views
0

我有一個2列的表,我使用PHP來動態生成表的行。每個錶行具有以下字段:如何使用Jquery在HTML表格的單擊表格行中選擇一個元素?

  • INPUT TYPE =「複選框」
  • 兩個輸入型=「文本」一個div裏面

我喜歡做的是隱藏包含股利當複選框被選中時,兩個輸入鍵入文本,我嘗試使用Jquery獲取單擊複選框的父級,然後隱藏當前內部的相應div,但它不起作用。

這是HTML代碼,看看如何生成我的表:

<table> 

<tr> 
    <th>Hide?</th> 
    <th>Inputs</th> 
</tr> 

<tr> 
    <td> 
     <input type="checkbox" name="checkBoxes[]"> 
    </td> 
    <td> 
     <div class="two-inputs"> 
      <input type="text" name="inputs[]"> 
      <input type="text" name="inputs[]"> 
     </div> 
    </td> 

</tr> 

<tr> 
    <td> 
     <input type="checkbox" name="checkBoxes[]"> 
    </td> 
    <td> 
     <div class="two-inputs"> 
      <input type="text" name="inputs[]"> 
      <input type="text" name="inputs[]"> 
     </div> 
    </td> 

</tr> 

+0

你嘗試過什麼?在Stackoverflow中有太多類似的問題。 – 2013-04-10 19:57:51

回答

1

嘗試。 jsfiddle

$("input:checkbox").click(function(){ 
    if(this.checked){ 
     $(this).parent().next().find("div").hide(); 
    } 
    else{ 
      $(this).parent().next().find("div").show(); 
    } 

}); 
+0

if-else可以簡化爲'$(this).parent()。next()。find(「div」)。toggle(!this.checked)' - ['.toggle()'](http: //api.jquery.com/toggle/); [小提琴](http://jsfiddle.net/M72Uw/1/) – Andreas 2013-04-10 20:04:28

+0

謝謝,它的工作原理! – Hedu911 2013-04-10 20:20:54

+0

@ Hedu911如果解決了您的問題,您應該將答案標記爲已接受。 – Anoop 2013-04-10 20:23:08

0
$('input:checkbox').click(function() { 
    if (!$(this).is(':checked')) { 
     $(this).parent().next('.two-inputs').hide(); 
    } 
}); 
相關問題