问题描述
|
所以我有一个表单,还有
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..\");
}
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>
它重定向。
关于如何使它起作用的任何想法?
解决方法
您需要从
reg_check
函数中移出ѭ8and,然后在onsubmit中将其更改为:
onsubmit=\"return reg_check(this);\"
这将取消表单提交。如果您想让表单正常提交,只需从just9ѭ返回true。
编辑(更清楚地说,您需要从函数中添加return false;):
function reg_check(myForm) {
alert(\"before redirect..\");
window.location.href = \"http://localhost/main.php?width=\" + screen.width + \"&height=\" + screen.height;
return false;
}
,我在IE中遇到了必须执行以下操作的问题:
window.location.assign(url);
特别是在jQuery AJAX成功处理程序中。真的无法推测为什么除了我之外,它还能奏效。
其实我做到了
window.location.replace(url)
它将替换历史记录中的当前页面。那太好了!
,我有同样的问题,但是在这里找到了答案Javascript:在function中调用function时,window.location.href不会重定向。
默认情况下,我的按钮正在重新加载页面,因此如果您添加
event.PreventDefault();
对于您的功能,它应该起作用。
,[解决]在从客户端脚本重定向到其他网址时,我遇到了类似的问题。而是实现了window.open函数,并且可以正常工作。例如,对于您的html控件事件,您可能有一个名为ChangeCity()的函数,该函数通过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;
/ *其他城市的案例* /
}
您可能想探索有关window.location.href重定向的详细信息-替代解决方案