[英]How to add with jQuery two or more rows with same ID on button click
I have a table with a list of products fetched from API and bellow table is a button.我有一张表格,其中列出了从 API 获取的产品列表,下面的表格是一个按钮。 I'm trying to achieve appending another table only with tr elements from the first table that have "product-row" ID.
我正在尝试仅使用第一个表中具有“产品行”ID 的 tr 元素来附加另一个表。 I'm using find() method and it works but I can't add it all at once, so I must click the button for each row.
我正在使用 find() 方法,它可以工作,但我不能一次全部添加,所以我必须单击每一行的按钮。 But when I put as parameter "tr" in find method it works.
但是当我在 find 方法中作为参数“tr”时它可以工作。 So my question is how to append another table with all rows with specific ID at once.
所以我的问题是如何 append 另一个表同时具有特定 ID 的所有行。
$("#tableBody").on("click", "#addToAspect", function(){
// #addToAspect is ID of button
var row = $("#tableBody").find("#product-row"); // this is ID of row
$("#aspectTable").append(row); // #aspectTable is second table
})
Only one unique ID on page - this is mandatory .页面上只有一个唯一 ID - 这是强制性的。
var row = $("#tableBody").find("#product-row");
- will always return only first found element with ID = product-row. - 将始终只返回 ID = product-row 的第一个找到的元素。
Can you replace #product-row with class.product-row?您可以用 class.product-row 替换 #product-row 吗? If you can't do anything with your code - I suggest use it in this way:
如果你不能对你的代码做任何事情 - 我建议以这种方式使用它:
$("#tableBody tr").each(function(index, elem){
if(elem.attr('ID') === 'product-row'){
$("#aspectTable").append(elem);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.