简体   繁体   English

如何防止在jQuery中额外的ajax调用?

[英]How to prevent extra ajax call in jQuery?

I have following code in asp.net (MVC) and jquery, where I have following html rendered on browser 我在asp.net(MVC)和jquery中有以下代码,在浏览器中呈现了以下html

<ul style="max-height: 250px; overflow: auto; width: 450px;" id="ulUNNotifications">
    <li>
        <div class="nolink">
            <p style="font-family: sans-serif; font-weight: normal; font-size: 14px; cursor: pointer;">Ticket#<a id="0" href="http://localhost/Web/xxxxx/Manage?TicketID=8310&amp" style="font-weight: normal;">8310</a> has been updated by client via email.</p>
            <p align="right" class="posted">3/18/2015 @ 3:55 PM</p>
        </div>
    </li>
    <li>
        <div class="nolink">
            <p style="font-family: sans-serif; font-weight: bold; font-size: 14px; cursor: pointer;">Ticket#<a id="1" href="http://localhost/Web/xxxxx/Manage?TicketID=8310&amp;">8310</a> has been updated by client via email.</p>
            <p align="right" class="posted">3/18/2015 @ 3:27 PM</p>
        </div>
    </li>
    <li>
        <div class="nolink">
            <p style="font-family: sans-serif; font-weight: normal; font-size: 14px; cursor: pointer;">Ticket#<a id="2" href="http://localhost/Web/xxxxx/Manage?TicketID=8310&amp;" style="font-weight: normal;">8310</a> has been updated by client via email.</p>
            <p align="right" class="posted">3/18/2015 @ 3:21 PM</p>
        </div>
    </li>
</ul>

Now My problem is that when I click on any of anchor tag it is making 3 time ajax call, means if there will 5 <li> then it will make 5 ajax call. 现在我的问题是,当我单击任何一个anchor标签时,它将进行3次ajax调用,这意味着如果将出现5个<li>则它将进行5次ajax调用。 I want to prevent extra ajax call. 我想防止额外的ajax电话。

I want want to make a ajax call for only clicked anchor tag. 我想只对单击的锚标记进行ajax调用。

I have used below jquery code to do so 我已经使用下面的jQuery代码做到这一点

$("#ulUNNotifications a:nth-child(1)").each(function (i) {
    var aHre = $(this).attr("href");

    $(this).bind("click", function () {
        $(this).css('font-weight', 'normal');

        fnShowDetails('' + aHre + '', $(this).text());

    });
});

function fnShowDetails(vHref, vticketid) {
    var tempScrollTop = $(window).scrollTop();
    $.ajax({
        url: '@Url.Action("GetDetails", "Tickets")' + '?troubleTicketIdstr=' + vticketid,
        type: "GET",
        success: function (data) {
            $('#dvDetails').css('display', 'block');
            $('#dvDetails').html(data);
            var overlay = jQuery('<div id="overlay"></div>');
            //overlay.appendTo("#dvTicketsDetails");
            $(window).scrollTop(tempScrollTop);
            $(".popup").css("left", (window.screen.width - $('.popup').width()) / 2);
            $(".popup").css("top", (window.innerHeight - $('.popup').height()) / 2);
            $("#dvTicketsDetails").fadeIn('slow');
            // $(".top_bord input[type=text]:first").focus();

        }
    });
    return false;
}

I want ajax call only for anchor tag clicked by user.No need to call for other tags also. 我只想对用户点击的锚标签进行ajax调用,也无需调用其他标签。

Replace the following code: 替换以下代码:

$("#ulUNNotifications a:nth-child(1)").each(function (i) {
    var aHre = $(this).attr("href");
    $(this).bind("click", function () {
        $(this).css('font-weight', 'normal');
        fnShowDetails('' + aHre + '', $(this).text());
    });
});

With: 带有:

$(document).on("click","#ulUNNotifications a",function(e){
     e.preventDefault();
     $(this).css('font-weight', 'normal');
     var aHre=$(this).attr('href');
     fnShowDetails('' + aHre + '', $(this).text());
});

Do you mean 你的意思是

$(function() {
  $("#ulUNNotifications").find("a").each(function (i) {
    $(this).on("click", function (e) {
       e.preventDefault(); // stop the link
       var aHre = $(this).attr("href");
       $(this).css('font-weight', 'normal');
       fnShowDetails(aHre, $(this).text());
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM