Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选中该行的复选标记时启用表中的元素_Javascript_Jquery_Html - Fatal编程技术网

Javascript 选中该行的复选标记时启用表中的元素

Javascript 选中该行的复选标记时启用表中的元素,javascript,jquery,html,Javascript,Jquery,Html,我有一个HTML表格。想象一下这样的情况(我将使用伪代码): 但是现在,因为它们都是内部标记,所以我不能使用该代码。有谁能告诉我我可以做些什么来更改此代码,使其与表一起工作吗?他们不再是兄弟姐妹了。因此,必须从一个公共父级遍历每个父级 $('[name*=chkd]').click(function(){ if ($(this).is(':checked')) { $(this).closest('tr').find('select').removeAtt

我有一个HTML表格。想象一下这样的情况(我将使用伪代码):


但是现在,因为它们都是内部标记,所以我不能使用该代码。有谁能告诉我我可以做些什么来更改此代码,使其与表一起工作吗?

他们不再是兄弟姐妹了。因此,必须从一个公共父级遍历每个父级

$('[name*=chkd]').click(function(){
        if ($(this).is(':checked')) {
            $(this).closest('tr').find('select').removeAttr('disabled');
        }
        else{
            $(this).closest('tr').find('select').attr('disabled', 'disabled');
        }
    });

假设您的标记如下所示:

<table>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>
      <select disabled>
        <option>A</option>
        <option>B</option>
      </select>
    </td>
    <td>
      <select disabled>
        <option>A</option>
        <option>B</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>
      <select disabled>
        <option>A</option>
        <option>B</option>
      </select>
    </td>
    <td>
      <select disabled>
        <option>A</option>
        <option>B</option>
      </select>
    </td>
  </tr>
</table>

参考资料:


输入的
不再是
选择的同级,因此您需要找到父级的同级,然后在这些元素中找到
选择的同级

$('[name*=chkd]')。单击(函数(){
var$sides=$(this.parent().sides().find('select');
如果($(this).is(':checked')){
$sides.removeAttr('disabled');
}否则{
$sides.attr('disabled','disabled');
}
});

asdf
asdf
asdf
asdf

您可以使用
find
搜索选定元素,并将禁用属性指定给复选框值:

$('.myTable').on('change', ':checkbox', function() {
  var $controls = $(this).closest('tr').find('select').prop('disabled', this.checked);
  if (this.checked) {
    $controls.val(0)
  }
}).find(':checkbox').change();
您可以检查

可能的副本
<table>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>
      <select disabled>
        <option>A</option>
        <option>B</option>
      </select>
    </td>
    <td>
      <select disabled>
        <option>A</option>
        <option>B</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>
      <select disabled>
        <option>A</option>
        <option>B</option>
      </select>
    </td>
    <td>
      <select disabled>
        <option>A</option>
        <option>B</option>
      </select>
    </td>
  </tr>
</table>
$(function() {
    // When a checkbox is clicked.
    $("input[type=checkbox]").on("click", function () {
        // Is it checked?
        var checked = $(this).is(":checked");

        // Look for the selects in the row and toggle their disabled property.
        $(this).closest("tr").find("select").prop("disabled", !checked);
  });
});
$('.myTable').on('change', ':checkbox', function() {
  var $controls = $(this).closest('tr').find('select').prop('disabled', this.checked);
  if (this.checked) {
    $controls.val(0)
  }
}).find(':checkbox').change();