首页 文章

如何使用Jquery在HTML表的单击表行中选择一个元素?

提问于
浏览
0

我有一个包含2列的表,我使用PHP生成动态行到表 . 每个表行都包含以下字段:

  • 输入类型= "checkbox"

  • div中的两个输入类型= "text"

我喜欢做的是在选中复选框时隐藏包含两个输入类型文本的div,我尝试使用Jquery获取单击复选框的父级,然后隐藏currrent <tr> 中的相应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>

2 回答

  • 1

    试试这个 . jsfiddle

    $("input:checkbox").click(function(){
       if(this.checked){
          $(this).parent().next().find("div").hide();
       }  
        else{
                 $(this).parent().next().find("div").show();
        }
    
    });
    
  • 0
    $('input:checkbox').click(function() {
        if (!$(this).is(':checked')) {
            $(this).parent().next('.two-inputs').hide();
        }
    });
    

相关问题