Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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/image-processing/2.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 - Fatal编程技术网

Javascript 获取表单元格相对于行的索引

Javascript 获取表单元格相对于行的索引,javascript,jquery,Javascript,Jquery,我在表的单元格中有一些复选框。我正在为它们绑定一个点击事件。单击后,我想获取当前单元格相对于父行的索引 例如: <tr> <td>...</td> <td><input type='checkbox'>....</td> </tr> 在这个JavaScript代码中,我希望当前单元格的索引是不起作用的I变量。$(this)在$(this)中。index()引用复选框,而不是单元格td 要获得单元格索引使用 $(

我在表的单元格中有一些
复选框
。我正在为它们绑定一个点击事件。单击后,我想获取当前单元格相对于父行的索引

例如:

<tr>
<td>...</td>
<td><input type='checkbox'>....</td>
</tr>
在这个JavaScript代码中,我希望当前单元格的索引是不起作用的
I
变量。

$(this)
$(this)中。index()
引用复选框,而不是单元格
td

要获得单元格索引使用

$(this).closest('td').index();  //or $(this).parent().index() if there is no further nesting.

另外,我建议您使用
.change()
事件,而不是
单击()

要获取可见元素索引

var $currentTr = $(this).closest('tr');
var  i = $('td:visible',$currentTr).index($(this).closest('td'))

您可以调用
td
$(this)。index()
将为您提供其父项(
td
)中
复选框的索引


您可以使用对目标复选框的parent()调用来访问列的索引。如果这是你的桌子的样子

$(“#网格输入[type='checkbox']”)。单击(函数(e){
var isChecked=$(this.prop('checked');
var$dataItem=$(this.parent();
var i=$dataItem.index();//给出任何行中的列号
$('#log')。追加(“”)。文本(i);
});

一排
第2排
第3排

请提问。请编辑您的问题,然后提问。这肯定有效。但我还有一个问题,如果你能帮助我,我将不胜感激。此表由剑道网格生成,每行有几个不可见的单元格。所以我得到的索引不是真正的索引。有解决办法吗?
Grid.tbody.find("input[type='checkbox']").each(function () {
    $(this).click(function () {        
        var isChecked = $(this).prop('checked');
        var dataItem = tablesGrid.dataItem($(this).closest('tr'));
        var i = $(this).closest('td').index();
        alert(i);
    });
});
var $currentTr = $(this).closest('tr');
var  i = $('td:visible',$currentTr).index($(this).closest('td'))
Grid.tbody.find("input[type='checkbox']").click(function () {
    var isChecked = this.checked;
    var dataItem = tablesGrid.dataItem($(this).closest('tr'));
    var i = $(this).closest('td').index();
    alert(i);
});