jQuery 超链接点击问题
在两种不同的视图中,我有以下两段代码。
第一
<table class="table-list">
<tr class="gridrow">
<td>David Gilmour</td>
<td style="width:16px">
<a href="#" rel="/xyz/Contact/Edit/26965" class="editContactLink" title="Modifica">
<img alt="" src="/xyz/_assets/images/edit.png">
</a>
</td>
</tr>
<tr class="gridrow">
<td>Paco De Lucia</td>
<td style="width:16px">
<a href="#" rel="/xyz/Contact/Edit/26966" class="editContactLink" title="Modifica">
<img alt="" src="/xyz/_assets/images/edit.png">
</a>
</td>
</tr>
</table>
第二
<div>
<a href="#" rel="/xyz/Contact/Edit/26965" class="editContactLink" title="Modifica">David Gilmour</a>
</div>
<div>
<a href="#" rel="/xyz/Contact/Edit/26966" class="editContactLink" title="Modifica">Paco De Lucia</a>
</div>
在这两种情况下,我都使用以下 jQuery 片段
$("a.editContactLink").click(function (event) {
event.preventDefault();
//use $(this).attr("rel") to go to the edit page of the related contact element
});
在第一种情况下,我只有一个对服务器的 ajax 调用,而在第二种情况下,我有两个 ajax 调用两者都去同一个地址
我哪里错了?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的代码在这里:
没有运行两次,无论是在另一个事件中,还是在开始时包含两次,等等。例如,如果它是由 AJAX 加载的,请更改
$("a.editContactLink")< /code> 到
$("a.editContactLink", data)
(data
是响应),或使用.live()
或.delegate()
处理程序,例如:Make sure that your code here:
isn't running twice, whether by being inside another event, or included twice to begin with, etc. For example if it's loaded by AJAX, change
$("a.editContactLink")
to$("a.editContactLink", data)
(data
being the response), or use a.live()
or.delegate()
handler, for example:这完全是一个黑客/创可贴,但它会让你工作。我毫无疑问地确信你绑定了两次,正如尼克所假设的那样。
This is totally a hack/band-aid, but it will get you working. I am indubitably sure that you are binding twice, as Nick assumed.
这个答案在这里只是为了报告错误场景...
我有一个返回地址列表的控制器
在AddressBookList部分视图中我只是有这样的代码
最后在AddressBook部分视图中我有
这就是多个的原因绑定。我将
我想我今天很累......
This answer is here only to report the error scenario...
I had a controller that return a list of addresses
In the AddressBookList partial view I simply had code like this
And finally in the AddressBook partial view I had
That was the reason for the multiple binding. I moved the
<script ...
section to the AddressBookList partial and everything works...I am tired for today I think.....