【问题标题】:On submit form, return false not working在提交表单上,返回 false 不起作用
【发布时间】:2013-08-28 05:24:28
【问题描述】:

当我提交表单时,我收到了一条警告消息。当我接受警报时,无论如何它都会提交表单。返回 false 将被忽略。 Onclick 无法使用。我尝试使用var x = document.forms["form"]["fname"].value; 还是一样。

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

【问题讨论】:

  • 它工作正常,检查浏览器控制台中的任何错误尝试jsfiddle.net/arunpjohny/KJyF8/1
  • 在这里工作正常:jsfiddle.net/su4jr
  • 看起来类似于 stackoverflow.com/questions/3013396/…>
  • 您的表单没有错误。它工作正常
  • 出于好奇,为什么对这个问题投了赞成票?

标签: javascript jquery html forms


【解决方案1】:

使用&lt;input type="button" value="Submit" onclick='validateForm()'/&gt; 代替&lt;input type="submit" value="submit"/&gt;

在你的 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>

【讨论】:

  • 问题说onclick不能用。
  • 问题是为什么不使用onclickonclick 什么的。
  • 点击按钮没问题。表单是问题,因为我有 4 个按钮。
  • @NejcGalof,请针对不同的科目提出不同的问题
【解决方案2】:

将此脚本放在 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>

【讨论】:

    猜你喜欢
    • 2018-10-18
    • 2011-09-23
    • 2013-10-20
    • 1970-01-01
    • 2017-05-13
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    相关资源
    最近更新 更多