我有一个表,其中有一个 thead
部分和一个 tbody
部分。我分别使用 jQuery 来查找和计算 table
中的所有 TH
。这很好用。但与此同时,我想检查 tbody
中 TH
的 TD
是否包含任何输入元素。
这是我目前所拥有的:
jQuery('#' + _target).each(function () {
var $table = jQuery(this);
var i = 0;
jQuery('th', $table).each(function (column) {
if (jQuery(this).find("input")) {
dataTypes[i] = { "sSortDataType": "input" }
}
else {
dataTypes[i] = { "sSortDataType": "html" }
}
i++;
});
});
我希望这些信息足以让您帮助我吗?
最佳答案
dataTypes = $('#' + _target + ' th').map(function(i, th) {
var $table = $(th).closest('table');
var pos = $table.index(th) + 1;
return { "sSortDataType": $table.find('td:nth-child('+pos+')').is(':has(input)') ? 'input' : 'html' };
});
编辑:如果您在单个表上运行它(在这种情况下,原始示例中的 each
调用没有多大意义),它会变得更简单:
dataTypes = $('#' + _target + ' th').map(function(pos, th) {
return { "sSortDataType": $(th).closest('table').find('td:nth-child('+(pos+1)+')').is(':has(input)') ? 'input' : 'html' };
});
关于javascript - JQuery.each() : find elements containing input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2810540/