开发者

how to escape jquery click event in Asp.net mvc action link?

开发者 https://www.devze.com 2023-02-27 05:07 出处:网络
<%: Html.ActionLink(\"Remove from cart\", \"RemoveFromCart\", new { id = item.RecordId }, new{@class =\"remove\" })%>
<%: Html.ActionLink("Remove from cart", "RemoveFromCart", new { id = item.RecordId }, new  {  @class ="remove" })%>

The rendering of this action is :

<a href="/JqueryTest/Delete/23" class="Delete-item">Delete</a>

And i have the JQuery code :

 $(document).ready(function () {
        $('a.remove').click(function () {
            $.ajax({
                type: "post",
                url: $(this).attr('href'),
                success: function (data) { alert('ok') },
                error: function () {
                    alert('error');
                },
                               });

What i want is, when i click delete link, I lunch an ajax request without responding to click e开发者_如何学编程vent (without goining in /JqueryTest/Delete/23 page ).

thanks


event.preventDefault() will prevent the default action of browser.

$('a.remove').click(function (event){
   event.preventDefault();
})

|Also $(this).attr('href') might not work inside ajax function, change it to

var myurl = $(this).attr('href');
$.ajax({
    type: "post",
    url: myurl,
    success: function (data) { alert('ok') },
    error: function () {
    alert('error');
    },
});


I think you can do a return false in the end of your click function.

0

精彩评论

暂无评论...
验证码 换一张
取 消