Javascript window.location.href 不以提交时的形式工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6109527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:15:51  来源:igfitidea点击:

window.location.href not working in form onsubmit

javascript

提问by Noura

So i have a form, and onsubmit="return reg_check(this)"where reg_check()is a javascript function in the header which is supposed to check the form data, and since one of its tasks is to check if the username is in the database which requires php, i want to redirect to a php page that does this task.

所以我有一个表单,标题中的 javascript 函数在onsubmit="return reg_check(this)"哪里reg_check()应该检查表单数据,并且由于它的任务之一是检查用户名是否在需要 php 的数据库中,我想重定向到执行此任务的 php 页面。

Problem is: window.location.hrefis not working! Here's the function (reduced version) and of course the main.phpis just a random page i got:

问题是:window.location.href不工作!这是函数(简化版),当然这main.php只是我得到的一个随机页面:

function reg_check(myForm) {
    alert("before redirect..");
    window.location.href = "http://localhost/main.php?width=" + screen.width + "&height=" + screen.height;
    alert("after redirect..");
}

The before redirectand after redirectalerts work, it just doesn't redirect? It remains in the same page.

before redirectafter redirect警示工作,它只是不重定向?它保留在同一页面中。

Also, if I tried to redirect from the body by just typing :

另外,如果我尝试通过键入以下内容从正文重定向:

<script type="text/javascript">
    alert("before redirect..");
    window.location.href = "http://localhost/main.php?width=" + screen.width + "&height=" + screen.height;
    alert("after redirect..");
</script>

it redirects.

它重定向。

Any ideas of how I could get this to work?

关于如何让它发挥作用的任何想法?

回答by Craig

You need to return false;from your reg_checkfunction and then in your onsubmit, change it to:

您需要return false;从您的reg_check功能中,然后在您的提交中,将其更改为:

onsubmit="return reg_check(this);"

This will cancel the form submission. And if you want to let the form submit as normal, just return true from reg_check.

这将取消表单提交。如果您想让表单正常提交,只需从reg_check.

Edit (to be more clear you need to add return false; from your function):

编辑(为了更清楚,你需要添加 return false; 从你的函数中):

function reg_check(myForm) {
    alert("before redirect..");
    window.location.href = "http://localhost/main.php?width=" + screen.width + "&height=" + screen.height;
    return false;
}

回答by Bret Weinraub

I've seen problems in IE where I've had to do the following:

我在 IE 中看到了一些问题,我不得不执行以下操作:

window.location.assign(url);

Specifically within a jQuery AJAX success handler. Can't really speculate to why other than for me, it worked.

特别是在 jQuery AJAX 成功处理程序中。除了对我来说,无法真正推测为什么它有效。

Actually I did

其实我做了

window.location.replace(url)

Which replaces the current page in the history. Nice trick that!

这将替换历史记录中的当前页面。不错的伎俩!

回答by pyathalon

I had the same problem, but found the answer here Javascript: window.location.href doesn't redirect when calling function within a function. My button was reloading the page by default so if you add an

我遇到了同样的问题,但在这里找到了答案Javascript: window.location.href 在函数内调用函数时不会重定向。默认情况下,我的按钮正在重新加载页面,因此如果您添加

 event.PreventDefault();

To your function it should work.

对于您的功能,它应该可以工作。

回答by K Raj

[RESOLVED] I had a similar problem in redirecting to some other url from client script. Implemented window.open function instead and it worked. You may have for instance, a function say ChangeCity() for your html control event that gets called with onchange event.

[已解决] 我在从客户端脚本重定向到其他一些 url 时遇到了类似的问题。改为实现 window.open 函数并且它起作用了。例如,您可能有一个函数说 ChangeCity() 用于您的 html 控件事件,该事件被 onchange 事件调用。

function ChangeCity() {
    switch ($("#currentCity").val()) {
        case "NY":
              var url = '@Url.Action("New York City", "Home", new { @area = "" },Request.Url.Scheme)';
            window.location.href = url;
            window.open(url,"_top");
            return false;

/* cases for other cities */

/* 其他城市的情况 */

    }

You may like to explore details on window.location.href redirection -Alternative Solution

您可能想探索有关window.location.href 重定向的详细信息 -替代解决方案