動的入力フィールドに検証を追加しようとしています。Jquery Validatorが定義されていません
<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>
}
スクリプト:
$('#AuthForm').removeData("validator");
$('#AuthForm').removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(this.authForm); // error here
プラグイン:
(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);
}
});
}
});
}
})($);
私は何をやっていることは、このように基本的に非表示と表示フィールドを(両方logingを有し、かつ1でフォームを登録する)でありますユーザーが
ToggleAuth
をクリックしたときに
だから、私は、フォーム内RegBoxStorage
からRegBox
にコンテンツを移動します。入力に検証を手動で追加する必要があります。すべての邪魔にならない検証属性は既にその上にあります。だからこのanswerから、私はそれを試して、$.validator is undefined
と言う。
しかし、フォームの他の部分が実際に検証され、検証が機能することを示し、Iはjquery.validation、参照jquery.validation.unobtrusive検証の両方を持っています。何が問題なの?
私はまた、このblogからプラグイン拡張を取りました。
は、あなたのjavascriptのコードを追加することはできますか? – llapinski
@llapinskiがコードを追加しました –
私は同じ問題を抱えていましたが、問題はMVC4が_Layout.cshtmlのデフォルトでバリデーションバンドルをレンダリングしなかったことです。 –