uncaugtht TypeError:undefined不是函数

时间:2014-07-17 07:43:39

标签: jquery

您好我正在尝试执行Basic jQuery Form Validation 参考此链接:

http://www.sitepoint.com/basic-jquery-form-validation-tutorial/

但是,我遇到了一些错误:uncaugtht TypeError: undefined is not a function 在这一行: $("#register-form").validate({

以下是代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css"
href="css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="css/myCSS1.css">
<script type="text/javascript"
src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<style type="text/css">
.bs-example{
    margin: 20px;
}
</style>


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

        JQUERY4U.UTIL =
        {
            setupFormValidation: function()
            {
                //form validation rules
                $("#register-form").validate({
                    rules: {
                        username: "required",
                        password: "required"
                     },
                    messages: {
                        username: "Please enter your username",
                        password: "Please enter your password"
                    },
                    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>

<div class="bs-example">
 <form id="register-form" class="form-horizontal" action="login" method="POST"  

  novalidate="novalidate">
    <fieldset >
        <div class="form-group">
            <label for="inputEmail" class="control-label col-xs-2">Username</label>
            <div class="col-xs-10">
                <input type="text" name="username" class="form-control" 

      id="inputEmail">
            </div>
        </div>
        <div class="form-group">
            <label for="inputPassword" class="control-label col-xs-2">Password</label>
            <div class="col-xs-10">
                <input name="password" type="password" class="form-control" 

          id="inputPassword" placeholder="Password">
            </div>
        </div>

        <div class="form-group">
            <div class="col-xs-offset-2 col-xs-10">
                <button type="submit" class="btn btn-primary">Login</button>
            </div>
        </div>
    </fieldset>
 </form>
 </div>
 </body>
 </html>  

2 个答案:

答案 0 :(得分:1)

validate不是内置的jQuery或bootstrap函数之一。你需要添加一个插件才能拥有它。查看代码,看起来好像需要this one

答案 1 :(得分:0)

您的标记中没有添加script。添加它,它就会起作用。

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

在该引用中,它告诉您使用此脚本,但我不知道为什么他们将其置于可选状态。我不认为JQuery有validate方法。