所以我有一个表格,并onsubmit="return reg_check(this)"在那里reg_check()为这是应该检查表单数据的头一个JavaScript函数,并且由于其任务之一是检查用户名是否在需要的PHP数据库,我想重定向到一个执行此任务的php页面.
问题是: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..");
}
Run Code Online (Sandbox Code Playgroud)
在before redirect和after redirect警示工作,它只是不重定向?它保留在同一页面中.
此外,如果我尝试通过键入以下内容重定向正文:
<script type="text/javascript">
alert("before redirect..");
window.location.href = "http://localhost/main.php?width=" + screen.width + "&height=" + screen.height;
alert("after redirect..");
</script>
Run Code Online (Sandbox Code Playgroud)
它重定向.
有关如何使其发挥作用的任何想法?
Cra*_*aig 38
您需要return false;从您的reg_check函数然后在您的onsubmit中,将其更改为:
onsubmit="return reg_check(this);"
Run Code Online (Sandbox Code Playgroud)
这将取消表单提交.如果您想让表单正常提交,只需返回true即可reg_check.
编辑(更清楚你需要添加return false;来自你的函数):
function reg_check(myForm) {
alert("before redirect..");
window.location.href = "http://localhost/main.php?width=" + screen.width + "&height=" + screen.height;
return false;
}
Run Code Online (Sandbox Code Playgroud)