jQuery 超链接点击问题

发布于 2024-10-08 19:37:58 字数 1439 浏览 3 评论 0 原文

在两种不同的视图中,我有以下两段代码。

第一

<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 调用两者都去同一个地址

我哪里错了?

In two different views I have the following two pieces of code.

FIRST

<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>

SECOND

<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>

In both cases I use the following jQuery snippet

$("a.editContactLink").click(function (event) {
    event.preventDefault();
    //use $(this).attr("rel") to go to the edit page of the related contact element
});

In the first case I have only one ajax call to the server while in the second I have two ajax calls both going to the same address

Where am I wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃素的狼 2024-10-15 19:37:58

确保您的代码在这里:

$("a.editContactLink").click(function (event) {
    event.preventDefault();
    //...
});

没有运行两次,无论是在另一个事件中,还是在开始时包含两次,等等。例如,如果它是由 AJAX 加载的,请更改 $("a.editContactLink")< /code> 到 $("a.editContactLink", data)data 是响应),或使用 .live().delegate() 处理程序,例如:

$("#container").delegate("a.editContactLink", "click", function(event) {
    event.preventDefault();
    //...
});

Make sure that your code here:

$("a.editContactLink").click(function (event) {
    event.preventDefault();
    //...
});

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:

$("#container").delegate("a.editContactLink", "click", function(event) {
    event.preventDefault();
    //...
});
笔落惊风雨 2024-10-15 19:37:58

这完全是一个黑客/创可贴,但它会让你工作。我毫无疑问地确信你绑定了两次,正如尼克所假设的那样。

$("a.editContactLink").unbind('click').click(function (event) {
    event.preventDefault();
    //use $(this).attr("rel") to go to t...
});

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.

$("a.editContactLink").unbind('click').click(function (event) {
    event.preventDefault();
    //use $(this).attr("rel") to go to t...
});
毁我热情 2024-10-15 19:37:58

这个答案在这里只是为了报告错误场景...

我有一个返回地址列表的控制器

[HttpGet]
public virtual ActionResult GetAddressBookEntries( int? page ) {
    IEnumerable<AddressBook> list = _manager.GetList();
    return PartialView( "AddressBookList", list );
}

在AddressBookList部分视图中我只是有这样的代码

<% foreach (var item in Model) { %>
    <% Html.RenderPartial( "AddressBook", item ); %>
<% } %>

最后在AddressBook部分视图中我有

<div class="ui-widget-header ui-corner-top" style="height:20px; padding:4px 6px;">
    <a href="#" rel='<%: Url.Content( "~/Contact/Edit/" ) + Model.ContactID %>' class="editContactLink" title="Modifica">
        <%: Model.FullName %>
    </a>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("a.editContactLink").click(function (event) {
            event.preventDefault();
            //use $(this).attr("rel") to go to t...
        });
    });
</script>

这就是多个的原因绑定。我将

我想我今天很累......

This answer is here only to report the error scenario...

I had a controller that return a list of addresses

[HttpGet]
public virtual ActionResult GetAddressBookEntries( int? page ) {
    IEnumerable<AddressBook> list = _manager.GetList();
    return PartialView( "AddressBookList", list );
}

In the AddressBookList partial view I simply had code like this

<% foreach (var item in Model) { %>
    <% Html.RenderPartial( "AddressBook", item ); %>
<% } %>

And finally in the AddressBook partial view I had

<div class="ui-widget-header ui-corner-top" style="height:20px; padding:4px 6px;">
    <a href="#" rel='<%: Url.Content( "~/Contact/Edit/" ) + Model.ContactID %>' class="editContactLink" title="Modifica">
        <%: Model.FullName %>
    </a>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("a.editContactLink").click(function (event) {
            event.preventDefault();
            //use $(this).attr("rel") to go to t...
        });
    });
</script>

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.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文