Javascript 从具有相同变量类的元素中获取值
张贴了一个类似的问题,但可能这个案例会更有意义。有一个表,有许多tr的第一个td,在不同的tr中有一个重复的第一个td。我用这些来识别。我想从第二个tr中提取一个值,并将其插入到第一个tr中已经附加的(!)td中Javascript 从具有相同变量类的元素中获取值,javascript,jquery,Javascript,Jquery,张贴了一个类似的问题,但可能这个案例会更有意义。有一个表,有许多tr的第一个td,在不同的tr中有一个重复的第一个td。我用这些来识别。我想从第二个tr中提取一个值,并将其插入到第一个tr中已经附加的(!)td中 <div class="tableclass"> <table> <tbody> <tr> <td>id1</td> <td>something el
<div class="tableclass">
<table>
<tbody>
<tr>
<td>id1</td>
<td>something else</td>
<td>1</td>
</tr>
<tr>
<td>id2</td>
<td>something else</td>
<td>2</td>
</tr>
<tr>
<td>id2</td>
<td>something else</td>
<td>3</td>
</tr>
<tr>
<td>id3</td>
<td>something else</td>
<td>4</td>
</tr>
<tr>
<td>id1</td>
<td>something else</td>
<td>5</td>
</tr>
<tbody>
</table>
<div>
id1
别的
1.
id2
别的
2.
id2
别的
3.
id3
别的
4.
id1
别的
5.
这是我的jquery代码
$(".tableclass table tbody tr").each(function(){
$(this).append('<td class="fbctr"></td>');
var trclass = $(this).find("td:first-child").html();
$(this).addClass(trclass);
// this is where I'm having a problem
var fbctr = $(this).parent().filter(trclass).eq(2).find("td:nth-child(3)");
$(this).find(".fbctr").html(fbctr);
});
$(“.tableclass表tbody tr”).each(函数(){
$(此)。附加(“”);
var trclass=$(this.find(“td:first child”).html();
$(this).addClass(trclass);
//这就是我遇到的问题
var fbctr=$(this.parent().filter(trclass.eq(2).find(“td:nth-child(3)”);
$(this.find(“.fbctr”).html(fbctr);
});
好的,您的问题是jquery
.filter()
在一组给定的DOM元素上工作。因此,您必须首先选择
元素,然后使用.filter()
。在您的问题中,您将其应用于您的
因此,您的JavaScript代码如下所示:
$(".tableclass table tbody tr").each(function(){
var trclass = $(this).find("td:first-child").html();
$(this).addClass(trclass);
var fbctr = $(this).parent().find('tr').filter('.'+trclass).eq(1).find("td:nth-child(3)").html();
if(typeof fbctr !== "undefined"){
console.log(fbctr);
$(this).find(".fbctr").html(fbctr);
}
});
更新(更正代码):
如果要将第一个出现元素的值复制到第二个出现元素中,请使用以下代码:
$(".tableclass table tbody tr").each(function(){
var trclass = $(this).find("td:first-child").html();
$(this).addClass(trclass);
var elements = $(this).parent().find('tr').filter('.'+trclass);
if(elements.length > 1){
var fbctr = elements.eq(0).find("td:nth-child(3)").html();
if(typeof fbctr !== "undefined")
$(this).find(".fbctr").html(fbctr);
}
});
$(".tableclass table tbody tr").each(function(){
var trclass = $(this).find("td:first-child").html();
$(this).addClass(trclass);
var elements = $(this).parent().find('tr').filter('.'+trclass);
var fbctr = elements.eq(1).find("td:nth-child(3)").html();
if(typeof fbctr !== "undefined")
$('.'+trclass).not(this).find(".fbctr").html(fbctr);
});
如果要将第二个出现元素的值复制到第一个出现元素中,请使用以下代码:
$(".tableclass table tbody tr").each(function(){
var trclass = $(this).find("td:first-child").html();
$(this).addClass(trclass);
var elements = $(this).parent().find('tr').filter('.'+trclass);
if(elements.length > 1){
var fbctr = elements.eq(0).find("td:nth-child(3)").html();
if(typeof fbctr !== "undefined")
$(this).find(".fbctr").html(fbctr);
}
});
$(".tableclass table tbody tr").each(function(){
var trclass = $(this).find("td:first-child").html();
$(this).addClass(trclass);
var elements = $(this).parent().find('tr').filter('.'+trclass);
var fbctr = elements.eq(1).find("td:nth-child(3)").html();
if(typeof fbctr !== "undefined")
$('.'+trclass).not(this).find(".fbctr").html(fbctr);
});
您可以先过滤掉重复的行并删除所有重复的行,但不能先删除,然后在第一行中添加最后一个重复行的第三个td元素 在下面的解决方案中,我还将删除重复的行,您可以根据需要保留/删除这些行 这是一个正在工作的解决方案,我在其中添加了几个额外的节点来测试该解决方案
var rows=$(“.tableclass表tbody tr”)
行。每个(函数(){
var trclass=$(this.find(“td:first child”).html();
$(this).addClass(trclass);
var dupeRows=rows.filter(“.”+trclass);
rows.filter(“.”+trclass).not(“:first”).remove();
如果(dupeRows.length>1){
rows.filter(“.”+trclass).first().append(“”+$(dupeRows[dupeRows.length-1]).find(“td:nth child(3)”).html()+“”);
}
});代码>
id1
别的
1.
id2
别的
2.
id2
别的
3.
id3
别的
4.
id1
别的
5.
id2
别的
5.
id2
别的
8.
;代码>你想得到什么?fbctr
的预期值是多少?因此,基本上您希望获得具有重复类的行的第三个单元格的内容,并将其复制到具有当前行的fbctr
类的子元素中?这就是您要查找的内容?@EhsanT yes。但是,当我用$(this.parent().find('tr').filter('.'+trclass).eq(1).find('td:nth child(3)'))替换有问题的行时;我得到了“jquery.min.js:3未捕获错误:语法错误,无法识别的表达式:.”jquery.min.js的错误通过这种方式,它将值插入到从OK获得值的同一个tr中,然后您想用第二个元素替换第一个元素的内容,或者反之亦然?也许我们可以采取另一种方法…我想从第二个tr中提取信息,并将其放入第一个tr中添加的td中。@hahna,刚刚更新了我的答案,您可以根据需要使用其中任何一种选项:)不客气…@hahna这有帮助吗?