href在表单onsubmit中不起作用

href在表单onsubmit中不起作用

本文介绍了window.location.href在表单onsubmit中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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 redirect and after redirect alerts work, it just doesn't redirect? It remains in the same page.

另外,如果我只是输入以下内容来尝试从正文重定向:

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>

重定向。

任何想法我怎么能让这个工作?

Any ideas of how I could get this to work?

推荐答案

您需要从<$ c $返回false; c> reg_check function然后在你的onsubmit中,将其更改为:

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

onsubmit="return reg_check(this);"

这将取消表单提交。如果你想让表单正常提交,只需从 reg_check 返回true。

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

编辑(将成为更清楚你需要添加return false;来自你的函数):

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

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

这篇关于window.location.href在表单onsubmit中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:24