Javascript 如何避免执行if条件后刷新页面
我用javascript编写了一个if条件,问题是,如果该条件为true,则语句(警报框)正在执行,如果单击警报框中的OK按钮,则整个网页将重新加载,并且每个控件中选择的值将返回到旧状态。代码如下。我试过“返回真”,但它不起作用Javascript 如何避免执行if条件后刷新页面,javascript,if-statement,reload,Javascript,If Statement,Reload,我用javascript编写了一个if条件,问题是,如果该条件为true,则语句(警报框)正在执行,如果单击警报框中的OK按钮,则整个网页将重新加载,并且每个控件中选择的值将返回到旧状态。代码如下。我试过“返回真”,但它不起作用 if(fromvalue==="" || tovalue1==="") { alert('Kindly select the origin and destination'); return true; }
if(fromvalue==="" || tovalue1==="")
{
alert('Kindly select the origin and destination');
return true;
}
else if(departuredate==="" || returndate==="")
{
alert('Kindly select the departure and return date');
}
else if(infantcountvalue>adultcountvalue)
{
alert("the infant number should be equal to or less than adult");
}
else if(departureday>returnday)
{
alert('Return date should be after the departure date');
}
else if(totalpassenger>9)
{
alert("Maximum 9 passengers are allowed.");
}
else
{
window.open(StrQueryString);
// submitform();
}
请张贴相关的HTML代码也。我打赌您正在使用表单,并且一旦提交表单,您就不会阻止提交按钮的默认操作,该按钮将提交页面,而不管if-else条件如何。Console.log()可以缩小导致该情况的原因,因为alert和return true;不会刷新页面。这是提交按钮的HTML将提交更改为常规按钮,我的意思是,只需执行以下操作:
,您就完成了。但是,请记住,您应该提交表单,而不是window.open,否则发送的所有值都将丢失。无论如何,您应该将整个HTML发布给我们;)