简体   繁体   English

在提交表单上,返回false不起作用

[英]On submit form, return false not working

When I submit the form I got an alert message. 当我提交表格时,我收到了一条警告信息。 When I accept the alert it will submit the form anyway. 当我接受警报时,无论如何都会提交表格。 Returning false is ignored. 返回false将被忽略。 Onclick can not be used. 不能使用Onclick。 I try with var x = document.forms["form"]["fname"].value; 我尝试使用var x = document.forms["form"]["fname"].value; and still same. 并且仍然相同。

<form id="f" method="post" name="form" onsubmit="return validateForm();" action="#">
    <input type="text" name="fname" id="test" />
    <input type="submit" value="submit"/>
</form>
<script type="text/javascript">
        function validateForm() {
            var x = document.getElementById('test').value;
            if (x == null || x == 0 || x == "0") {
                alert("Stop");
                return false;
            }
        }
    </script>

Instead of <input type="submit" value="submit"/> use <input type="button" value="Submit" onclick='validateForm()'/> . 而不是<input type="submit" value="submit"/>使用<input type="button" value="Submit" onclick='validateForm()'/>

In your JS: 在你的JS中:

<script type="text/javascript">
    function validateForm() {
        var x = document.getElementById('test').value;
        if (x == null || x == 0 || x == "0") {
            alert("Stop");
        }
        else
            document.form.submit();
    }
</script>

Give this script inside of head tag and check it. 将此脚本放在head标签内并检查它。

<script type="text/javascript">
        function validateForm() {
            var x = document.getElementById('test').value;
            if (x == null || x == 0 || x == "0") {
                alert("Stop");
                return false;
            }
        }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM