[英]jQuery selector to find TR that contains TD INPUT with specific value
Ok, this seems like it should be a simple question, but the answer eludes me. 好吧,这似乎应该是一个简单的问题,但答案就是我。 I have a form that is built from JSON data from which I build a table of 3 rows and 14 columns each containing an INPUT text control.
我有一个从JSON数据构建的表单,我从中构建了一个包含INPUT文本控件的3行和14列的表。
When the value in any of those controls changes I need to find the containing row whose input control in the first column equals a specific year. 当任何控件中的值发生变化时,我需要找到第一列中输入控件等于特定年份的包含行。
Here is a sample of the markup: 以下是标记示例:
<table id="MyTable">
<tr>
<td><input type="text" /></td>
<td><input type="text" class="changeHandled"/></td>
<td><input type="text" class="changeHandled" /></td>
</tr>
</table>
The selector I've tried is: #MyTable tr:has('input[value="{year}"]')
and a few dozen variations. 我尝试过的选择器是:
#MyTable tr:has('input[value="{year}"]')
和几十种变体。
I replace the {year}
string with the year value I'm searching for, so it ends up looking like: #MyTable tr:has('input[value="2014"]')
我将
{year}
字符串替换为我正在搜索的年份值,因此最终看起来像: #MyTable tr:has('input[value="2014"]')
I believe it may be due to the fact the values are set through code initially, because if I use #MyTable tr:has('input')
it returns all of the rows. 我相信它可能是由于最初通过代码设置值的事实,因为如果我使用
#MyTable tr:has('input')
它将返回所有行。
Because of maintainability I don't want to hardcode the path and just say $(this).closest("tr")...
in javascript code. 由于可维护性,我不想硬编码路径,只是在javascript代码中说
$(this).closest("tr")...
Any idea how to retrieve a reference to the TR containing an input with a specific value? 知道如何检索包含具有特定值的输入的TR的引用吗? If my code is correct, do I need to do something different when setting the value via code?
如果我的代码是正确的,在通过代码设置值时是否需要做一些不同的事情?
The selector $('#MyTable tr:has(input[value="2014"])')
will only work if the input
element has a hardcoded value
attribute with a value of 2014
. 选择器
$('#MyTable tr:has(input[value="2014"])')
仅在input
元素具有值为2014
的硬编码value
属性时才有效。 See this example . 看这个例子 。
If you are trying to select an input
element without a hardcoded value
attribute, then you could filter over the elements and return input
elements whose value equals 2014
. 如果您尝试选择没有硬编码
value
属性的input
元素,则可以筛选元素并返回其值等于2014
input
元素。 Depending on your needs, you could listen to the input
or change
event. 根据您的需要,您可以收听
input
或change
事件。
$('#MyTable input').on('input change', function () {
$tr = $('#MyTable tr input:first').filter(function () {
return this.value === '2014' || $(this).attr('value') === '2014';
}).closest('tr');
});
If you were to use a plan JS solution it might look like: 如果您使用计划JS解决方案,它可能看起来像:
function getRowByYear(year) {
var table = document.getElementById('MyTable');
var row, rows = table.rows;
var input;
for (var i=0, iLen=rows.length; i<iLen; i++) {
input = rows[i].cells[0].getElementsByTagName('input')[0];
if (input.value == year) {
return rows[i];
}
}
}
and would probably run very much faster than a jQuery selector. 并且可能比jQuery选择器运行得快得多。
However, since you say " When the value in any of those controls changes… " then perhaps you are using a change event, so you can simply get the input from which the event was dispatched ( this within the listener) and go up to the nearest row. 但是,既然你说“ 当任何一个控件中的值发生变化...... ”那么你可能正在使用一个更改事件,所以你可以简单地获取调度事件的输入( 这在侦听器中)并转到最近的一排。
Though I agree that vanilla JS would be more efficient, I think filtering is too expensive? 虽然我同意vanilla JS会更有效率,但我觉得过滤太贵了? I'd go with listening for the change event and finding the closest row:
我会去听取更改事件并找到最近的行:
$( 'body' ).on( 'change', 'input[type="text"]', function(){
var theRow = $( this ).closest( 'tr' );
// Do what you need to with theRow
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.