我有一个表,其中一些有我需要运行验证的输入字段。验证后,我将一个类附加.mandatory到空白输入字段。现在稍后我需要遍历td有.mandatory孩子的 s 并从表格的相应th元素中获取相应的文本/html。我认为最好的办法是找到索引td并使用它来获得正确th的索引,但我不知道如何有效地获取索引td。我尝试了类似的东西
$('.mandatory').each(function(){
var ind = curr_tr.find('>td').index($(this).closest('td'));//curr_tr is a reference to the current tr I am working with
var txt = $('th:eq('+ind+')').html();
})
但这似乎效率低下,如果某些.mandatory字段嵌套在td