JavaScript警报错误消息

我正在对表单进行简单的验证. javascript验证工作正常,弹出警告框出现正确的错误,但是,点击“确定”后,我会重新定向到下一页.理想情况下,它应该保持在同一页面,以便用户可以修改他/她的错误.请尽快帮助,这是一个学校项目,截止日期为3天! :( 提前致谢.

<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>

解决方法:

这应该很容易修复.在表单标记的onsubmit方法中,执行以下操作:

<form onsumbit="return show_alert();">

代替

 <form onsumbit="show_alert();">

如果没有返回部分,脚本将运行,无论如何都将提交表单.此外,如果脚本中存在错误情况,则需要添加返回false;否则表格仍会提交,并返回true;如果没有错误.您可以像这样编辑脚本:

<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>
上一篇:如何使用android webview中的弹出窗口


下一篇:Javascript:关闭弹出窗口