I'm trying to find a specific row in a table, where the td value in the first column is the_month and the td value in the second column is the_year.
我试图在表中找到一个特定的行,其中第一列中的td值是__thth,第二列中的td值是the_year。
var the_month = 3,
the_year = 15;
var tableRow = jQuery("td").filter(function() {
return jQuery(this).text() == the_month;
}).siblings().filter(function() {
return jQuery(this).text() == the_year;
}).parent('tr')
This code is almost what I want, but it's looking at every td. How can I limit this to only look for the_month in column 1 and the_year in column 2.
这段代码几乎就是我想要的,但它正在查看每一个td。如何将此限制为仅查找第1列中的__th和第2列中的the_year。
Edit: I understand the lack of clarity now. I have a huge table. I'm trying to find the row where the value of the first column is equal to the variable I created, called the_month, which is a number, and the value of the second column is the_year, which is also a variable I created. These variables are numbers. The code I originally posted keeps finding the value of the_month, let's say the_month = 3, in other columns of the wrong row. So I want to look for a row that has the number 3 in the first column and the number 15 in the second column.
编辑:我理解现在缺乏清晰度。我有一张大桌子。我试图找到第一列的值等于我创建的变量的行,称为the_month,这是一个数字,第二列的值是the_year,它也是我创建的变量。这些变量是数字。我最初发布的代码一直在错误行的其他列中查找the_month的值,比如the_month = 3。所以我想查找第一列中编号为3而第二列中编号为15的行。
2 个解决方案
#1
If your code works as it stands, and I'm understanding you correctly, instead of making your query search for month and year, you should make it search for month then year
如果您的代码按原样运行,并且我正确理解您,而不是让您的查询搜索月份和年份,那么您应该使其搜索月份然后年份
I haven't used filters, but I've made what you're looking for (I think) in THIS FIDDLE
我没有使用过滤器,但是我已经在这个FIDDLE中找到了你想要的东西(我想)
Here's the accompanying code:
这是随附的代码:
$('tr').each(function(i){
var month = $(this).find('td').eq(0).text();
if(month == 'the_month'){
var year = $(this).find('td').eq(1).text();
if(year == 'the_year'){
var tableRow = i;
console.log(i);
}
}
});
Now, since you didn't have any html posted, I can only assume what I've written is what you meant.
现在,由于你没有发布任何html,我只能假设我写的是你的意思。
#2
Try this. This would have your first and second td's filtered out. http://jsfiddle.net/66za2f8n/2/
试试这个。这将使您的第一个和第二个td被过滤掉。 http://jsfiddle.net/66za2f8n/2/
//you can modify the select to be #idofTable tr
var tableRow = $("table tr").filter(function() {
return $(this).find("td").eq(0).text() == "the_month" &&
$(this).find("td").eq(1).text() == "the_year";
});
console.log(tableRow);
#1
If your code works as it stands, and I'm understanding you correctly, instead of making your query search for month and year, you should make it search for month then year
如果您的代码按原样运行,并且我正确理解您,而不是让您的查询搜索月份和年份,那么您应该使其搜索月份然后年份
I haven't used filters, but I've made what you're looking for (I think) in THIS FIDDLE
我没有使用过滤器,但是我已经在这个FIDDLE中找到了你想要的东西(我想)
Here's the accompanying code:
这是随附的代码:
$('tr').each(function(i){
var month = $(this).find('td').eq(0).text();
if(month == 'the_month'){
var year = $(this).find('td').eq(1).text();
if(year == 'the_year'){
var tableRow = i;
console.log(i);
}
}
});
Now, since you didn't have any html posted, I can only assume what I've written is what you meant.
现在,由于你没有发布任何html,我只能假设我写的是你的意思。
#2
Try this. This would have your first and second td's filtered out. http://jsfiddle.net/66za2f8n/2/
试试这个。这将使您的第一个和第二个td被过滤掉。 http://jsfiddle.net/66za2f8n/2/
//you can modify the select to be #idofTable tr
var tableRow = $("table tr").filter(function() {
return $(this).find("td").eq(0).text() == "the_month" &&
$(this).find("td").eq(1).text() == "the_year";
});
console.log(tableRow);