如果空字段如何停止表单提交?

时间:2012-06-25 15:36:36

标签: php jquery forms

我的网站仅基于一个php索引文件,该文件根据标头变量更改网站内容。 www.mydomain.com/?page=contact加载并显示我正在填写用户的表单。

我有一些jQuery 应该检测是否有任何遗漏字段:

$("form").submit(function() {
    var isFormValid = true;
    $(".required").each(function() {
        if ($.trim($(this).val()) == "") {
            $(this).addClass("highlight");
            isFormValid = false;
        } else {
            $(this).removeClass("highlight");
        }
        if (!isFormValid) alert("Please fill in all the required fields (indicated by *)");
        return isFormValid;
    });
});

但它没有用......

3 个答案:

答案 0 :(得分:4)

return isFormValid置于循环之外(否则您将一次又一次覆盖其值):

$("form").submit(function() {
    var isFormValid = true;
    $(".required").each(function() {
        if ($.trim($(this).val()) == "") {
            $(this).addClass("highlight");
            isFormValid = false;
        } else {
            $(this).removeClass("highlight");
        }

    });

    if (!isFormValid) alert("Please fill in all the required fields (indicated by *)");
    return isFormValid; // put out of loop
});

答案 1 :(得分:3)

也许是这样的?

$("form").submit(function() {
    var isFormValid = true;
    $(".required").each(function() {
        if ($.trim($(this).val()) == "") {
            $(this).addClass("highlight");
            isFormValid = false;
        } else {
            $(this).removeClass("highlight");
        }
    });
    // the following lines should be placed here
    if (!isFormValid) {
        alert("Please fill in all the required fields (indicated by *)");
    }
    return isFormValid;
});

甚至更短:

$("form").submit(function() {
    var isFormValid = $(".required").removeClass("highlight").filter(function() {
        return $.trim(this.value) == "";
    }).addClass("highlight").length == 0;

    if (!isFormValid) {
        alert("Please fill in all the required fields (indicated by *)");
    }
    return isFormValid;
});

DEMO: http://jsfiddle.net/YS6gw/

答案 2 :(得分:1)

Can you post the html works perfectly fine for me. 
Here is my Code :
------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>

    <script type="text/javascript"  src="http://code.jquery.com/jquery-latest.min.js"></script>


    <script type="text/javascript">
        $(document).ready(function(){
            $("form").submit(function() {
                var isFormValid = true;
                $(".required").each(function() {
                    if ($.trim($(this).val()) == "") {
                        $(this).addClass("highlight");
                        isFormValid = false;
                    } else {
                        $(this).removeClass("highlight");
                    }
                    if (!isFormValid) alert("Please fill in all the required fields (indicated by *)");
                    return isFormValid;
                });
            });

        });

    </script>

    <style type="text/css">
        #divMain {vertical-align: bottom;}
        .div1_style {background: #ffe4b5}
        .div2_style {background: #79b7e7}
        .highlight {background: #cd0a0a}
        .div_disabled_style {background: #6f6e73}
    </style>
</head>
<body>
<form action="#" method="post">
<div id="divMain" align="center">
    <div id="div1" class="div2_style">
        <input type="checkbox" id="checkBox1">
        <input type="text" class ="required"  id="text1"/>
        <input type="text" class ="required" id="text2"/>
        <input type="text" class ="required" id="text3"/>
        <button type="submit" id="btnSubmit">Submit</button>
    </div>
</div>
</form>
</body>
</html>