我正在嘗試將驗證添加到動態輸入字段。我在做什麼基本上是隱藏和顯示字段(同時具有登錄電子和1登記表格)是這樣的: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);
}
});
}
});
}
})($);
因此,當用戶點擊ToggleAuth
,我從RegBoxStorage
所述內容移動到RegBox
形式內。現在我需要手動將驗證添加到輸入。所有不顯眼的驗證屬性都已經在其上。所以從這answer,我試了一下,並說$.validator is undefined
。
但形式的其他部分實際驗證,表明驗證的作品,我都jquery.validation和參考jquery.validation.unobtrusive驗證。可能是什麼問題呢?
我也參加了該插件擴展從這個blog。
您可以將您的javascript代碼? – llapinski
@llapinski添加了代碼 –
我有同樣的問題,問題是MVC4默認在_Layout.cshtml中不呈現validate bundle。 –