简体   繁体   English

如何使用 jQuery 在按钮单击时添加具有相同 ID 的两行或多行

[英]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.

相关问题 如何连接单击添加按钮创建的同一组件中的两个组件 - How to connect two components in a same component created on click of add button Jquery 单击按钮,然后在第二页上触发具有相同 Class 或 ID 的按钮 - Jquery Click button then trigger a button on second page with the same Class or ID 使用jquery多次对同一个id执行click功能 - Perform click function on same id at more than once using jquery 单击 jQuery 显示两个 HTML 表中的更多行 - Show more rows across two HTML tables on click with jQuery 如何通过单击按钮并动态增加行数来向html表添加两个文本框文本? - How to add two text boxes texts to html table with button click and increment rows dynamically? 两个jQuery函数侦听相同的按钮单击,但只有一个起作用 - two jQuery functions listening to the same button click but only one works 如何遍历 class 名称,因此如果两个或多个元素具有相同的数据属性,则它们可以使用 jQuery 具有相同的 ID - How to iterate through class name, so if two or more elements have the same data attributes they can have the same ID using jQuery 单击具有相同ID的每个按钮 - Click each button with the same ID 如何在量角器中单击同一按钮超过50次? - How to click the same button more than 50 times in protractor? 如何使用相同的按钮切换两个元素(无 JQuery) - How to toggle two elements with the same button (no JQuery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM