0

我正在尝试实现此处找到的简单表单验证插件:http ://www.jquery4u.com/forms/basic-jquery-form-validation-tutorial/

但我似乎无法让它工作。

我有一个简单的表格:

<form id="signupform" name="signupform" action="page" method="post" novalidate="novalidate">
<input type="text" id="signupusername" name="signupusername">
<input type="text" id="signupemail" name="signupemail">
<input type="password" id="signuppassword" name="signuppassword">
<input type="text" id="signupfirstname" name="signupfirstname">
<input type="text" id="signuplastname" name="signuplastname">
<input type="checkbox" name="tandc" value="tandcyes"> I agree
<button type="submit" value="Signup" class="submit">Signup</button>
</form>

以及相关的插件脚本:

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">
(function($,W,D)
{
    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        setupFormValidation: function()
        {
            //form validation rules
            $("#signupform").validate({
                rules: {
                    signupusername: "required",
                    signupfirstname: "required",
                    signuplastname: "required",
                    signupemail: {
                        required: true,
                        email: true
                    },
                    signuppassword: {
                        required: true,
                        minlength: 5
                    },
                    tandcyes: "required"
                },
                messages: {
                    signupusername: "Please enter your User Name",
                    signupfirstname: "Please enter your firstname",
                    signuplastname: "Please enter your lastname",
                    signuppassword: {
                        required: "Please provide a password",
                        minlength: "Your password must be at least 5 characters long"
                    },
                    signupemail: "Please enter a valid email address",
                    tandcyes: "Please accept our policy"
                },
                submitHandler: function(form) {
                    form.submit();
                }
            });
        }
    }

    //when the dom has loaded setup form validation rules
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
    });

})(jQuery, window, document);

但它只是行不通。

当然,我已经检查了插件和指向 JQuery 的链接是否存在且正确。控制台中也没有出现任何内容。它似乎没有触发。

有谁知道发生了什么事?

谢谢!

4

2 回答 2

3

问题是您包含了 2 次相同的脚本。

带有验证的脚本中的函数在源代码中出现了 2 次。浏览器很难知道选择哪一个。

检查 head 是否有两个脚本。

请以表单标签中不存在的形式添加所有输入。

<form>
<input type=... />
<input type=... />
</form>

如果您在输入之前关闭表单,那就是问题所在。

于 2013-01-23T07:12:59.200 回答
1

使用此页面及其作品

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>


<title>Basic jQuery Validation Form Demo | jQuery4u</title>
<link rel="stylesheet" type="text/css" href="bootstrap.css">

<script type="text/javascript">
(function($,W,D)
{
    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        setupFormValidation: function()
        {
            //form validation rules
            $("#signupform").validate({
                rules: {
                    signupusername: "required",
                    signupfirstname: "required",
                    signuplastname: "required",
                    signupemail: {
                        required: true,
                        email: true
                    },
                    signuppassword: {
                        required: true,
                        minlength: 5
                    },
                    tandcyes: "required"
                },
                messages: {
                    signupusername: "Please enter your User Name",
                    signupfirstname: "Please enter your firstname",
                    signuplastname: "Please enter your lastname",
                    signuppassword: {
                        required: "Please provide a password",
                        minlength: "Your password must be at least 5 characters long"
                    },
                    signupemail: "Please enter a valid email address",
                    tandcyes: "Please accept our policy"
                },
                submitHandler: function(form) {
                    form.submit();
                }
            });
        }
    }

    //when the dom has loaded setup form validation rules
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
    });

})(jQuery, window, document);
</script>

</head>
<body>

<h1>Basic jQuery Validation Form Demo</h1>

<!-- HTML form for validation demo -->
<form id="signupform" name="signupform" action="page" method="post" novalidate="novalidate">

<input type="text" id="signupusername" name="signupusername">
<input type="text" id="signupemail" name="signupemail">
<input type="password" id="signuppassword" name="signuppassword">
<input type="text" id="signupfirstname" name="signupfirstname">
<input type="text" id="signuplastname" name="signuplastname">
<input type="checkbox" name="tandc" value="tandcyes"> I agree
<button type="submit" value="Signup" class="submit">Signup</button>

</form>
<!-- END HTML form for validation -->

</body>

</html>
于 2013-01-23T06:50:30.290 回答