jquery 验证错误:.validate 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19823998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:40:20  来源:igfitidea点击:

jquery validation error: .validate is not a function

jqueryjquery-validate

提问by Jen Born

This is probably super obvious, but it is NOT working! I'm getting: TypeError: $(...).validate is not a functionand Firebug points me to:

这可能是非常明显的,但它不起作用!我得到:TypeError: $(...).validate is not a function和 Firebug 指向我:

email: "Please enter a valid email address",

email: "请输入一个有效的电子邮件地址",

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script>

$(document).ready(function () {

    $("#register").click(function () {
        if ($("#frmRegisterWebuser").valid() === true) {
            $("#frmRegisterWebuser").submit();
        }
    });

    $("#frmRegisterWebuser").validate({
      rules:{
             reg_email:{
                        required: true,
                        email: true
                        },
             username: {
                       required: true,
                       minlength: 3
                       },
             zipcode: {
                       minlength: 5,
                       maxlength: 5
                       },
             reg_password: {
                       required: true,
                       minlength: 5
                       },
             confpass: {
                       required: true,
                       equalTo: "#reg_password",
                       }
      },
      messages:{
         username: {
                   required: "Please enter a username",
                   minlength: "Your username must consist of at least 3 characters"
                   },
         password: {
                   required: "Please provide a password",
                   minlength: "Your password must be at least 5 characters long"
                   },
        confirm_password: {
                          required: "Please provide a password",
                          minlength: "Your password must be at least 5 characters long",
                          equalTo: "Please enter the same password as above"
                          },
        email: "Please enter a valid email address",
      },

      submitHandler:function(){
          if($("#frmRegisterWebuser").valid() === true){
            email=$("#reg_email").val();
            password=$("#reg_password").val();
            username=$("#username").val();
            zipcode=$("#zipcode").val();
            default_private=$("#private").val();

             $.ajax({
                type: "POST",
                url: "webuser_register.php",
                data: "email="+email+"&pwd="+password+"&username="+username+"&default_private="+default_private+"&zipcode="+zipcode,
                success: function(response){
                  if(response == 'Registered')
                  {
                    $("#login_form").fadeOut("normal");
                $("#shadow").fadeOut();
                $("#profilex").html("<a id='logout' href='webuser_logout.php' title='Click to Logout'>Logout</a>");

                $("#shadow").slideUp();
                $("#register_webuser").slideUp();
                  }
                  else
                  {
                    $("#reg_add_err").html("Oops, there was a problem");
                  }
                },
                beforeSend:function()
                        {
                     $("#reg_add_err").html("Loading...")
                }
            });
          }
      }

    });

});
</script>

Could it be that I'm not actually submitting a form, but am ajaxing some data?

可能是我实际上不是在提交表单,而是在 ajaxing 一些数据?

<div id="register_webuser">
        <div class="err" id="reg_add_err"></div>
  <form id="frmRegisterWebuser" action="login.php">
    <input type="hidden" id="private" name="private" />
        <label style="color:black;" for="reg_email" >Email:</label>
        <input type="email" id="reg_email" name="reg_email"  />
        <label style="color:black;" for="username" >Username:</label>
        <input type="text" id="username" name="username"  />
        <label style="color:black;" for="zipcode" >Zipcode:</label>
        <input type="text" id="zipcode" name="zipcode" />
        <label style="color:black;" for="reg_password" >Password:</label>
        <input type="password" id="reg_password" name="password"  />
        <label style="color:black;" for="confpass" >Confirm Password:</label>
        <input type="password" id="confpass" name="confpass"  />

        <div class="inputdata">
        <label style="color:black;" title="Share your shopping lists">Share:&nbsp;</label>
        <span>
        <div class="flatRoundedCheckbox" style="v-align:top;">
        <input type="checkbox" title="Share your shopping lists" id="share" name="share"  checked onclick="toggleShared(this.checked);" />
        </span>
        <label for="share" id="label_share"></label>
        <div class="clear"></div>
        </div>
        </div>

        <label></label><br/>
        <a id="register" href="#" class="greenbutton" >Register</a>
        <a id="reg_cancel_hide" href="#" class="greenbutton" >Cancel</a>
        <label></label><br/>

  </form>
</div>

回答by Jen Born

I'm the biggest dummy ever. Nonetheless, perhaps this will help someone else.

我是有史以来最大的假人。尽管如此,也许这会对其他人有所帮助。

The issue was that I was including 2 different versions of jQuery on the same page: both 1.9.0 and 1.10.2

问题是我在同一页面上包含了 2 个不同版本的 jQuery:1.9.0 和 1.10.2

Thanks everyone so much for your help!

非常感谢大家的帮助!

回答by Siddharth

In my case I was loading jquery 2 times. Commented one and started to work.

就我而言,我加载了 2 次 jquery。评论了一个并开始工作。

<script         
    src="<c:url value="/resources/plugins/jQuery/jQuery-2.1.4.min.js"/>">
</script>