gpt4 book ai didi

c# - JQuery 无法识别点击表行

转载 作者:太空宇宙 更新时间:2023-11-03 11:22:45 25 4
gpt4 key购买 nike

我正在为我的表格数据使用 JQuery 模板...并且 CSS 识别查询模板脚本中的表格行并按照我想要的方式设置它们的样式...但由于某种原因我不能调用 $ ().click 使用相同标识符的相同行。这是表格代码:

<script id="donorTemplate" type="text/x-jquery-tmpl">
<tr id="indexTR">
<td>${Person.LastName}, ${Person.FirstName}</td>
<td>${Address.Address1}</td>
<td>${PhoneContact.PhoneNumber}</td>
<td>${EmailContact.EmailContactx}</td>
<td>${Company.CompanyName}</td>
<td><a href="/Donation/Index?id=${Person.PersonID}">Add Donation</a></td>
<td> <a href="/Person/Edit?id=${Person.PersonID}">Edit</a> &nbsp; <a href="/Person/Delete?
id=${Person.PersonID}">Delete</a> &nbsp;
<input type="hidden" id="hiddenField" value="${Person.PersonID}"/>
</td>
</tr>
</script>

<div id="searchresults">
<table id="donor-list">
<thead>
<tr>
<th id="f" width="150">Name: </th>
<th width="180">Address: </th>
<th width="85">Phone: </th>
<th width="150">Email: </th>
<th width="100">Company: </th>
<th width="100"></th>
<th id="l" width="100"></th>
</tr>
</thead>
<tbody id="tableBody">

</tbody>
</table>

还有 JQuery....

$("#donorSearch").submit(function (event) {
event.preventDefault();
var form = $(this);
$.getJSON(form.attr("action"), form.serialize(), function (data) {
$("#tableBody #indexTR").remove();
$("#donorTemplate").tmpl(data).appendTo("#tableBody");
});
});

这就是花花公子......但这不起作用:

$("#indexTR").click(function () {
alert('test');
});

这就是我尝试对 TR 点击执行的操作:

var val = $("#hiddenField").val();
var personID = parseInt(val);
location.href = "/Person/Details?id=" + personID;

在我切换到 JQuery 模板之前一直有效。有什么建议么?谢谢!

最佳答案

<tr id= "#indexTR">准备就绪时 DOM 中不存在,因此使用委托(delegate)事件应该可以解决问题:

$('#donor-list').on('click', '#indexTR', function(){
alert('test');
});

对于 jQuery 1.5:

$('#donor-list').delegate('#indexTR', 'click', function(){
alert('test');
});

或者在每一个新创建的行上附加回调。

$.getJSON(form.attr("action"), form.serialize(), function (data) {
$("#tableBody #indexTR").remove();
$("#donorTemplate").tmpl(data).appendTo("#tableBody");
// Now attach the event:
$("#indexTR").click(function () {
alert('test');
});
});

你遇到了多个元素具有相同 id 的问题.正如您在问题的评论中所讨论的那样。

关于c# - JQuery 无法识别点击表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406208/

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