JavaScript 警报后页面重新加载/刷新 - 不想这样做!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2092090/
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
Page re-load/refresh after JavaScript alert - dont want it do!
提问by Etienne
My JavaScript function is working but for some reason after the alert is displayed inside my IF statement the page re-loads/refresh and I do not want it to. Why is this and how can I change my function so that it will not do this?
我的 JavaScript 函数正在工作,但由于某种原因,在我的 IF 语句中显示警报后,页面会重新加载/刷新,但我不希望它这样做。为什么会这样,我该如何更改我的功能以使其不执行此操作?
My function
我的功能
function valSubmit(){
varName = document.form1.txtName.value;
varSurname = document.form1.txtSurname.value;
varEmail = document.form1.txtEmail.value;
varOrg = document.form1.txtOrg.value;
if (varName == "" || varSurname == "" || varEmail == "" || varOrg == "" )
{
alert("Please fill in all mandatory fields");
}
else
{
document.body.style.cursor = 'wait';
document.form1.btnSubmit.style.cursor = 'wait';
document.form1.action = "http://now.eloqua.com/e/f2.aspx"
document.form1.submit();
return true;
}
}
p.s I am using ASP.NET 3.5
ps 我使用的是 ASP.NET 3.5
回答by Fenton
Here is your complete function with the return false statement added.
这是添加了 return false 语句的完整函数。
Additionally, when you call valSubmit, it should look like this:
此外,当您调用 valSubmit 时,它应该如下所示:
... onsubmit="return valSubmit();"...
... onsubmit="返回 valSubmit();"...
Note, you need to specify return here also.
请注意,您还需要在此处指定 return。
Here is the function:
这是函数:
function valSubmit(){
varName = document.form1.txtName.value;
varSurname = document.form1.txtSurname.value;
varEmail = document.form1.txtEmail.value;
varOrg = document.form1.txtOrg.value;
if (varName == "" || varSurname == "" || varEmail == "" || varOrg == "" )
{
alert("Please fill in all mandatory fields");
return false;
}
else
{
document.body.style.cursor = 'wait';
document.form1.btnSubmit.style.cursor = 'wait';
document.form1.action = "http://now.eloqua.com/e/f2.aspx"
document.form1.submit();
return true;
}
}
回答by Erik
you have to add return false;to stop the default action from taking place from the form submit
您必须添加return false;以阻止从表单提交中发生的默认操作
回答by Kniganapolke
I guess you should add return false;after your alert, because the form goes on submitting.
我想您应该return false;在您的 之后添加alert,因为表单会继续提交。
回答by Elyor
Just ad return false after alert. Like:
只是广告在警报后返回 false。喜欢:
alert("Please fill in all mandatory fields");
return false;

