如何防止HTML.ActionLink上的默认值? [英] How can I preventDefault on HTML.ActionLink?

查看:126
本文介绍了如何防止HTML.ActionLink上的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的母版页中,我具有以下ActionLink:

In my master page I have the following ActionLink:

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

从该母版页继承的我看来,我定义了以下click事件:

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
});

当前,当用户单击链接时,重置状态"链接将使用NULL参数向我的控制器操作发出GET请求. click事件永远不会引发,并且事件默认值不会停止(因此它将出现).上面的javascript已添加到我的视图的页脚中.

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.

使用Firebug,我可以看到该事件绑定在$(document).ready()上.此时,在页面上找到了$(#resetButton").当事件绑定时,我使用手表确保了这一点.

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

关于为什么我的js可能无法触发的任何想法?

Any ideas on why my js might not be firing?

谢谢

无视

我是傻瓜.我在脚本中发现了一个js错误.

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

推荐答案

我通常添加return false以防止点击事件的默认行为:

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
});

如果仍然不能阻止默认行为,则您的事件未绑定,这意味着在调用document.ready时,resetButton不存在.

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.

这篇关于如何防止HTML.ActionLink上的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆