繁体   English   中英

jQuery添加规则时验证抛出错误

[英]Jquery validate throwing error when adding a rule

当我的js页面加载并创建如下所示的规则时,我从jquery.validate.js库第138行引发了错误,该错误表示“无法读取未定义的属性'设置'”

settings = $.data( element.form, "validator" ).settings;

这是我要添加的规则以及我也要添加的表单

$('#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运行时错误:无法获取未定义或空引用的属性“设置”

必须首先通过在表单元素上调用.validate()方法来初始化插件。

$('#yourform').validate({ // <-- INITIALIZE the plugin on your form
    // any options, rules, or callbacks
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM