简体   繁体   中英

How to select a element in the clicked table row of a HTML table using Jquery?

I have a table with 2 columns, I'm using PHP to generate dynamically rows to the table. Every table row have the following fields:

  • Input Type="checkbox"
  • Two Inputs type="text" inside a div

What I like to do is to hide the div containing the two inputs type text when the checkbox is checked, I tried to get the parent of the clicked checkbox using Jquery and then hide the corresponding div inside the currrent <tr> but it's not working.

This is the HTML code to see how is my table generated:

<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>

Try This. jsfiddle

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM