在我的MVC4應用程序中,我試圖爲表單中的某些文本框創建客戶端驗證器。該表單包含在Register.cshtml視圖中(當我使用Formsauthentication項目製作新的MVC4時,此視圖由VS2010自動創建)。JavaScript驗證不在mvc應用程序中工作
該表單已經在提交按鈕上實現了驗證,但是我想要一個驗證器,只要焦點離開文本框就行動 - 應該向我顯示文本框旁邊的內聯錯誤,並帶有紅色字體。如果確認無誤,則應再次顯示消息「確定」。
問題:.js驗證程序不起作用。 Firebug說:
$ document is not defined。 $的document.ready(
我也試過警報(),如果我把它的document.ready功能外,它彈出時裏面,它不彈出 代碼registerForm.js:。
$document.ready(
function()
{
$('#registration form').validate
(
{
rules:
{
UserName: { required: true }
},
success: function (label)
{
label.Text('OK!').addClass('IsInputValid');
}
}
)
}
);
,增加了此JavaScript是如下束:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/registerForm.js",
"~/Scripts/jquery.validate*"));
最後,視圖:
<div id="registration">
@using (Html.BeginForm()) {
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
}
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
什麼可以防止js工作? 我已經花了大約5小時試圖弄清楚這一點。請讓我知道如果您有任何建議,
問候,
您是否閱讀過該消息? – SLaks