未定义Jquery Validator

Sha*_*ean 3 asp.net-mvc jquery jquery-validate asp.net-mvc-3

我正在尝试将验证添加到动态输入字段.我正在做的基本上是隐藏并显示一个字段(在1中同时具有登录和注册形式),如下所示:

<div id="RegBoxStorage" style="display: none;">
    @Html.LabelFor(m => Model.Name)
    @Html.TextBoxFor(m => Model.Name)
    @Html.ValidationMessageFor(m => m.Name)
</div>
@using (Html.BeginForm("Login", "Auth", FormMethod.Post, new { id = "AuthForm" }))
{
<div id="RegBox" style="display: none;">
</div>
<div id="LoginBox">

    @Html.LabelFor(m => Model.Email)
    @Html.TextBoxFor(m => Model.Email)
    @Html.ValidationMessageFor(m => m.Email)

    @Html.LabelFor(m => Model.Password)
    @Html.TextBoxFor(m => Model.Password)
    @Html.ValidationMessageFor(m => m.Password)
</div>
<input type="submit" id="BtnAuthSub" value="Login" />
or <a id="ToggleAuth" href="#">Sign Up</a>
}
Run Code Online (Sandbox Code Playgroud)

剧本:

$('#AuthForm').removeData("validator");
$('#AuthForm').removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(this.authForm); // error here
Run Code Online (Sandbox Code Playgroud)

插件:

(function ($) {
  $.validator.unobtrusive.parseDynamicContent = function (selector) {
    //use the normal unobstrusive.parse method
    $.validator.unobtrusive.parse(selector); //Error here

    //get the relevant form
    var form = $(selector).first().closest('form');

    //get the collections of unobstrusive validators, and jquery validators
    //and compare the two
    var unobtrusiveValidation = form.data('unobtrusiveValidation');
    var validator = form.validate();

    $.each(unobtrusiveValidation.options.rules, function (elname, elrules) {
      if (validator.settings.rules[elname] == undefined) {
        var args = {};
        $.extend(args, elrules);
        args.messages = unobtrusiveValidation.options.messages[elname];
        //edit:use quoted strings for the name selector
        $("[name='" + elname + "']").rules("add", args);
      } else {
        $.each(elrules, function (rulename, data) {
          if (validator.settings.rules[elname][rulename] == undefined) {
            var args = {};
            args[rulename] = data;
            args.messages = unobtrusiveValidation.options.messages[elname][rulename];
            //edit:use quoted strings for the name selector
            $("[name='" + elname + "']").rules("add", args);
          }
        });
      }
    });
  }
})($);
Run Code Online (Sandbox Code Playgroud)

因此,当用户点击时ToggleAuth,我将内容从表单RegBoxStorage移到RegBox内部.现在我需要手动将验证添加到输入中.所有不显眼的验证属性都已经存在.所以从这个答案,我尝试并说$.validator is undefined.

但是表单的其他部分实际上已经过验证,表明验证工作正常,我同时引用了jquery.validation和jquery.validation.unobtrusive验证.可能是什么问题呢?

我也从这个博客中获取了插件扩展.

Mor*_*ncz 7

检查几件事:

  • 您已编写扩展名但仍在"脚本使用"部分中$.validator.unobtrusive.parse(this.authForm)- 请确保使用parseDynamicContent.

  • 确保在插件代码之前引用validation.js和validation.unobtrusive.js.

  • 确保你的Script部分在jQuery document.ready函数内,并在插件后执行.

  • 在我的情况下,事实证明我一直认为我有正确的脚本,但实际上它是**jquery.unobtrusive-ajax**而不是**jquery.validate.unobtrusive**....花了我一会儿在我发现它之前. (3认同)