简体   繁体   English

以下语法有什么问题:它不起作用

[英]What is wrong in following syntax: Its not working

I am trying to navigate to another page using the following syntax: 我正在尝试使用以下语法导航到另一个页面:

                    $("#Button1").click(function () {
        alert("I am raised because of click event");
        window.location = "../Home/Index";
        alert("I am exiting click event");
    });

Its entering into the click event handler. 它进入点击事件处理程序。 Its firing both the alert messages. 它会触发两个警报消息。 But, the page is refershing intead of redirecting to another page.(index view) 但是,该页面是重定向到另一个页面的参考界面。(索引视图)

Is there any alternatitve way of doing this? 有什么替代方法吗? I am using aspx View engine. 我正在使用aspx View引擎。

The button probably also cause a ASP.NET postback, which makes the page reload. 该按钮可能还会导致ASP.NET回发,从而使页面重新加载。

return false from your click handler to prevent this. 从您的点击处理程序返回false可以防止这种情况。

$("#Button1").click(function () {
    alert("I am raised because of click event");
    window.location = "../Home/Index";
    alert("I am exiting click event");
    return false;
});

Asp.net's button control <asp:button> will cause page to postback. Asp.net的按钮控件<asp:button>将导致页面回发。 So you could do 2 things: 因此,您可以做两件事:

1) As Mikael said you could return false in your click handler, 2) Or you could use an html input control for your button to prevent the postback. 1)正如Mikael所说,您可以在点击处理程序中返回false,2)或者您可以为按钮使用html输入控件以防止回发。 ( <input type="button" /> ) unless you have other requirements to use asp:button. <input type="button" /> ),除非您有其他要求使用asp:button。

you need to use 你需要使用

window.location.href="your path";

Hope it helps. 希望能帮助到你。

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

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