JavaScript表单错误状态不在同一页上
我正在做一个表单的简单验证。JavaScript验证工作正常,弹出警告框显示正确的错误,但单击“确定”后,我将被重新定向到下一页。理想情况下,它应该保持在同一页上,以便用户可以修改他/她的错误 这是一个学校项目JavaScript表单错误状态不在同一页上,javascript,popup,alert,Javascript,Popup,Alert,我正在做一个表单的简单验证。JavaScript验证工作正常,弹出警告框显示正确的错误,但单击“确定”后,我将被重新定向到下一页。理想情况下,它应该保持在同一页上,以便用户可以修改他/她的错误 这是一个学校项目 <script type = "text/javascript"> function show_alert() { if (document.getElementById('time1').value == document.getElementById('time
<script type = "text/javascript">
function show_alert() {
if (document.getElementById('time1').value == document.getElementById('time2').value) alert("ERROR! You cannot book the same timing twice!")
else if (document.getElementById('time1').value == document.getElementById('time3').value) alert("ERROR! You cannot book the same timing twice!")
else if (document.getElementById('time1').value == document.getElementById('time4').value) alert("ERROR! You cannot book the same timing twice!")
else if (document.getElementById('time1').value == "0") alert("ERROR! You cannot leave the first time slot blank!")
else {}
}
</script>
函数show_alert(){
if(document.getElementById('time1').value==document.getElementById('time2').value)警报(“错误!不能预订两次相同的计时!”)
else if(document.getElementById('time1').value==document.getElementById('time3').value)警报(“错误!您不能预订两次相同的计时!”)
else if(document.getElementById('time1')。value==document.getElementById('time4')。value)警报(“错误!您不能预订两次相同的计时!”)
else if(document.getElementById('time1')。value==“0”)警报(“错误!您不能将第一个时间段留空!”)
else{}
}
这应该很容易修复。在表单标记的onsubmit方法中,执行以下操作:
<form onsumbit="return show_alert();">
另外,
show\u alert
需要返回true或false,指示是否应提交表单。@casablanca是的-我在您留下评论时添加了这一点…在回答时分心了,意外地遗漏了它。:-)@杰里米,我刚试过,但不在同一页上。它仍然会转到下一页。这是按钮的代码:这是表格的代码:你能给我举个例子吗。也许我可以把文件发电子邮件给你?非常感谢:D@Chandni如果将提交按钮置于
标记的onsubmit
部分,则不需要提交按钮上的onclick=“show_alert()”
。此外,请检查我的更新答案,以了解所需的脚本修改。
<form onsumbit="show_alert();">
<script type = "text/javascript">
function show_alert() {
if (document.getElementById('time1').value == document.getElementById('time2').value) {
alert("ERROR! You cannot book the same timing twice!");
return false;
} else if (document.getElementById('time1').value == document.getElementById('time3').value) {
alert("ERROR! You cannot book the same timing twice!");
return false;
} else if (document.getElementById('time1').value == document.getElementById('time4').value) {
alert("ERROR! You cannot book the same timing twice!");
return false;
} else if (document.getElementById('time1').value == "0") {
alert("ERROR! You cannot leave the first time slot blank!");
return false;
} else {
return true;
}
}
</script>