开发者

How to run Jquery on click of an ActionLink

开发者 https://www.devze.com 2023-04-07 10:25 出处:网络
How do I run jquery when an MVC ActionLink is clicked? I have an action that takes some time. I want to show a 开发者_如何学运维\'processing\' image until it returns. Is there a better way to do this?

How do I run jquery when an MVC ActionLink is clicked? I have an action that takes some time. I want to show a 开发者_如何学运维'processing' image until it returns. Is there a better way to do this?


Give the actionlink a class:

@Html.ActionLink("","", new { @class:"delete" })

Then you can hook up your jQuery to the class name in a js file:

$(document).ready(function() {
  $('.delete').click(function() {
    ///Code
  });
});

You can also work with id.


You can also use @Ajax.ActionLink without need of jQuery script. For example you can show a div when loading a page.

<div>
@Ajax.ActionLink("Link name", "Action", "Controller", new AjaxOptions { LoadingElementId = "loadingId", UpdateTargetId = "MyDataTable" })
</div>

<div id="loadingId" style="display:none; color:Red; font-weight: bold">
<p>Please wait...</p>
</div>

<div id = "MyData" class="tablediv">
    <table id = "MyDataTable" class="Grid">
        <tbody id="chartdata">
        </tbody>
    </table>
</div>


$('#element-id').live('click', function(e) {
   //do stuff here ...
});

The trick is to give your action link an ID HTML attribute so you can use it as your element id:

$('#html-attribute-id').live('click', function(e) {
   //do stuff here ...
});
0

精彩评论

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

关注公众号