2

我的网站仅基于一个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;
    });
});

但它不工作....

4

3 回答 3

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
});
于 2012-06-25T15:38:04.347 回答
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;
});

演示:http: //jsfiddle.net/YS6gw/

于 2012-06-25T15:38:16.620 回答
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>
于 2012-06-25T15:58:06.487 回答