开发者

How can I preventDefault on HTML.ActionLink?

开发者 https://www.devze.com 2023-03-19 16:32 出处:网络
In my master page I have the following ActionLink: <%=Html.ActionLink(\"Reset State\", \"ResetState\", \"State\", null, new { id = \"resetButton\" })%>

In my master page I have the following ActionLink:

 <%=Html.ActionLink("Reset State", "ResetState", "State", null, new { id = "resetButton" })%>

In my view, which inherts from this master page, I have the following click event defined:

 $(document).ready(function () {
    $("#resetButton").click(function (e) {
        var context = org.testportal.context;
        var appId = context.currentApplicationId;
        var userId = context.currentUserId;
        var hwid = context.currentHwid;

        if (contextIsValid()) {
            $.get('/State/ResetState', { applicationId: appId, userId: userId, hwid: hwid },
   function (data) {
       openDialogBox(data);
   }, "json");
        }
        else {
            var data = { message: "Invalid Context" };
            openDialogBox(data);
        }
        e.preventDefault();
    }); //resetState
});

Currently, when the user clicks the link, "Reset State" the link makes GET request to my controller action with NULL parameters. The click event is never raised and the event default is not stopped (so it would appear). The above javascript is added in the footer of my view.

Us开发者_运维技巧ing Firebug I can see that the event is bound on $(document).ready(). At this time $("#resetButton") is found on the page. I made sure of this using a watch when the event is bound.

Any ideas on why my js might not be firing?

Thanks

Disregard

I'm a dingle. I found a js error in the script.


I normally add return false in order to prevent the default behavior of a click event:

$(document).ready(function () {
    $("#resetButton").click(function (e) {
        var context = org.testportal.context;
        var appId = context.currentApplicationId;
        var userId = context.currentUserId;
        var hwid = context.currentHwid;

        if (contextIsValid()) {
            $.get('/State/ResetState', { applicationId: appId, userId: userId, hwid: hwid },
   function (data) {
       openDialogBox(data);
   }, "json");
        }
        else {
            var data = { message: "Invalid Context" };
            openDialogBox(data);
        }
        return false;
    }); //resetState
});

If that is still not preventing the default behavior then your event is not binding, which means your resetButton is not there when the document.ready is called.


If you have an anchor with id='link', this how you may prevent the default behavior:

$('#linl').click(function (e) { e.preventDefault(); }

If you want all hyperlinks of specific kind to have this behavior:

$('a[name=modal]').click(function (e) {            
  e.preventDefault();
  ....
}

Here any <a name="modal" ...>link</a> will have the default behavior prevented.

This is how I load modal window off of hyper links.

Hope it helps.

Thanks

0

精彩评论

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