Jquery在添加规则时验证抛出错误 [英] Jquery validate throwing error when adding a rule
本文介绍了Jquery在添加规则时验证抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我的js页面加载时,我创建了一个规则如下所示,我得到了从jquery.validate.js库行#138抛出的错误,它说无法读取属性'设置'的未定义
settings = $ .data(element.form,validator).settings;
以下是我添加的规则以及我将其添加的表单
$('#zipCodeText')。rules(add,{
required:true,
minlength:5,
maxlength:5,
messages:{
required:必需的输入,
minlength:jQuery.validator.format(Please, 0}字符是必要的)),
maxlength:jQuery.validator.format(请,{0}个字符是必须的)
}
});
@using(Html。 BeginForm(SaveLocation,Manage,FormMethod.Post,new {@class =form-horizontal,@id =registerForm,role =form})){
< div class = 形式的基团>
< label for =countryList>国家< / label>
< br /> ;@Html.EnumDropDownListFor(m => m.Countries,null,new {@id =countryList,@class =selectpicker btn-group-lg})
< / div>
< label for =zipCodeText> ZipCode< / label>
< label id =zipCodeMessagestyle =display:none;>< / label>
< / div>
< label for =locationText>位置< / label>
< label id =locationMessagestyle =display:none;>< / label>
< / div>
< input type =buttonvalue =Saveid =yogaLocationSaveclass =btn btn-primary btn-lg/>
< input type =buttonvalue =Cancelid =yogaLocationCancelclass =btn btn-default btn-lg/>}
解决方案
JavaScript运行时错误:无法获取未定义或null引用的属性设置
您必须先初始化插件,方法是调用
.validate()
方法。$('#yourform') .validate({// < - INITIALIZE表单上的插件
//任意选项,规则或回调
});
When my js page loads and I create a rule seen below I get a error thrown from the jquery.validate.js library line #138, that says "cannot read property 'settings' of undefined"
settings = $.data( element.form, "validator" ).settings;
Here is the rule I'm adding and the form I'm adding it too
$('#zipCodeText').rules("add", { required: true, minlength: 5, maxlength: 5, messages: { required: "Required input", minlength: jQuery.validator.format("Please, {0} characters are necessary"), maxlength: jQuery.validator.format("Please, {0} characters are necessary") } });
@using (Html.BeginForm("SaveLocation", "Manage", FormMethod.Post, new { @class = "form-horizontal", @id = "registerForm", role = "form" })) { <div class="form-group"> <label for="countryList">Country</label> <br />@Html.EnumDropDownListFor(m => m.Countries, null, new { @id = "countryList", @class = "selectpicker btn-group-lg" }) </div> <div id="zipCodeGroup" class="form-group has-feedback" style="display: @(Model.Country == 0 ? " " : "none ");"> <label for="zipCodeText">ZipCode</label> <input type="number" min="5" max="5" class="form-control input-lg" id="zipCodeText" placeholder="e.g. 94102" value="@Model.ZipCode"> <label id="zipCodeMessage" style="display: none;"></label> </div> <div id="locationGroup" class="form-group has-feedback" style="display: @(Model.Country == 0 ? " none " : " ");"> <label for="locationText">Location</label> <input type="text" min="4" max="40" class="form-control input-lg" id="locationText" placeholder="e.g. San Francisco" value="@Model.Location"> <label id="locationMessage" style="display: none;"></label> </div> <input type="button" value="Save" id="yogaLocationSave" class="btn btn-primary btn-lg" /> <input type="button" value="Cancel" id="yogaLocationCancel" class="btn btn-default btn-lg" />}
解决方案JavaScript runtime error: Unable to get property 'settings' of undefined or null reference"
You must first initialize the plugin by calling the
.validate()
method on your form element.$('#yourform').validate({ // <-- INITIALIZE the plugin on your form // any options, rules, or callbacks });
这篇关于Jquery在添加规则时验证抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文