gpt4 book ai didi

javascript - 如何获取使用Jquery动态添加的HTML Table行记录?

转载 作者:行者123 更新时间:2023-12-03 05:06:03 26 4
gpt4 key购买 nike

I want to get the row records, which is added dynamically. I am getting an output if I add the row using html.

开始时表中没有记录。这是我的代码。

HTML 代码

<table class="table table-hover " id="queryTable">
<thead>
<tr>
<th>Field Name</th>
<th>Values</th>
</tr>
</thead>
<tbody>

</tbody>
</table>

Table with empty row At beginning当用户点击添加按钮时,将使用 jquery 成功添加行

After Added rows by user clicking the ADD ButtonJQUERY代码

$('#queryTable > tbody:last-child').append('<tr><td class="FieldNameID">' + selectedField + '</td><td class="OperatorID"> IN(' + $("#txtFilterValue").val() + ')</td></tr>');

Upto this it's working fine. Now, When user clicks on any row, I want to select the row and show it in alert box, which is added dynamcally.

最佳答案

WORKING FIDDLE

动态添加的元素,必须这样绑定(bind)事件。

$(document).on("click",'#queryTable tbody tr',function(){
alert($(this).html());
});

仅显示td

 $(document).on("click",'#queryTable tbody tr',function(){
var tr=$(this);
var firsttd=tr.find("td:first").text();
var secondtd=tr.find("td:last").text(); //BECAUSE YOU HAVE ONLY 2 TDs
alert(firsttd + ' ' + secondtd);
});

UPDATED FIDDLE

关于javascript - 如何获取使用Jquery动态添加的HTML Table行记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41996691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com