Javascript 如何使用jquery在表中禁用行和列复选框(单击的复选框除外)

Javascript 如何使用jquery在表中禁用行和列复选框(单击的复选框除外),javascript,jquery,checkbox,disabled-input,Javascript,Jquery,Checkbox,Disabled Input,我已尝试使用以下代码。我已禁用行中的复选框。现在,禁用相应列时出现问题 在下面的代码片段中找到我的HTML和JS代码 代码片段 $('input[type=checkbox]')。单击(function(){ $(this).最近('tr')) .find('input[type=checkbox]')。而不是(this) .prop('disabled',this.checked); }); 8-9 9-10 10-11 11-12 12-13 13-14 14-15 15-16 公司名称

我已尝试使用以下代码。我已禁用行中的复选框。现在,禁用相应列时出现问题

在下面的代码片段中找到我的HTML和JS代码

代码片段
$('input[type=checkbox]')。单击(function(){
$(this).最近('tr'))
.find('input[type=checkbox]')。而不是(this)
.prop('disabled',this.checked);
});

8-9
9-10
10-11
11-12
12-13
13-14
14-15
15-16
公司名称1
公司名称2
公司名称3
公司名称4
您可以使用获取单击的单元格的列索引

如果没有向.index()方法传递任何参数,则返回值是一个整数,指示jQuery对象中第一个元素相对于其同级元素的位置

然后,您可以使用
:n子项
查找该列中的所有单元格:

工作小提琴:

(问题代码使用了
无线电
,但需要一个取消勾选的选项,因此更改为复选框)

您可以使用该选项获取单击的单元格的列索引

如果没有向.index()方法传递任何参数,则返回值是一个整数,指示jQuery对象中第一个元素相对于其同级元素的位置

然后,您可以使用
:n子项
查找该列中的所有单元格:

工作小提琴:


(问题代码使用了
无线电
,但需要取消勾选,因此改为复选框)

您可以这样做:

$('input[type=checkbox]').click(function(){
    $(this).closest('tr')
    .find('input[type=checkbox]').not(this)
    .attr('disabled', this.checked);


    // Below is the added code
    var tdIndex = $(this).closest('td').index() + 1;
    $('table').find("tr td:nth-of-type(" + tdIndex + ")")
    .find('input[type=checkbox]').not(this)
    .attr('disabled', this.checked);

});
在本地测试并按预期工作。将为小提琴更新


更新:这是小提琴

你可以这样做:

$('input[type=checkbox]').click(function(){
    $(this).closest('tr')
    .find('input[type=checkbox]').not(this)
    .attr('disabled', this.checked);


    // Below is the added code
    var tdIndex = $(this).closest('td').index() + 1;
    $('table').find("tr td:nth-of-type(" + tdIndex + ")")
    .find('input[type=checkbox]').not(this)
    .attr('disabled', this.checked);

});
在本地测试并按预期工作。将为小提琴更新


更新:这是列的fiddle,您需要获取列索引

所以…像这样的

var cell = $(this).closest('td');
var index = $(this).closest('tr');
var table = $(this).closest('table');
$(table).find('tr td:nth-child(' + index + ') input[type=checkbox]').not(this).prop('disabled', this.checked);

对于列,您需要获取列索引

所以…像这样的

var cell = $(this).closest('td');
var index = $(this).closest('tr');
var table = $(this).closest('table');
$(table).find('tr td:nth-child(' + index + ') input[type=checkbox]').not(this).prop('disabled', this.checked);

由于这是一个字体结束问题,你能考虑用代码制作一个片段吗?(用HTML替换PHP部分)嗨,Pierre,我用HTMLHTML替换了PHP代码。TML代码包含单选按钮。您的JavaScript包含“input[type=checkbox]”。。。奇怪的由于这是一个字体结束问题,你能考虑用代码制作一个片段吗?(用HTML替换PHP部分)嗨,Pierre,我用HTMLHTML替换了PHP代码。TML代码包含单选按钮。您的JavaScript包含“input[type=checkbox]”。。。奇怪的