我正在尝试对 Razor View 内的字段使用自定义 JQuery 验证,但我不知道如何将该字段与 JQuery 函数关联,我已经使用过 JQuery 但来自 html 页面和表单,而不是使用 Razor View 中的 HtmlHelpers 这是我想要验证的字段(我在同一页面中引用我需要的 JQuery .js)
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
我尝试在其中放入一个 id 并将 EditorFor 更改为 TextBoxFor,然后从我的 JQuery 函数调用它,但没有成功
@Html.TextBoxFor(model => model.Name, new { id="artistName", htmlAttributes = new { @class = "form-control" } })
这是我在 JQuery 中使用的函数,但我找不到如何将它绑定(bind)到 Html Helper:
$(document).ready(function () {
$("#artistName").on("blur",function(){
if ($(this).val().match('^[a-zA-Z]{3,16}$')) {
alert("Valid name");
}
else {
alert("That's not a name");
}
})
})
最佳答案
删除 id 属性并使用 $("#Name") 作为选择器。
@Html.TextBoxFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
$("#Name").on("blur",function(){
if ($(this).val().match('^[a-zA-Z]{3,16}$')) {
alert("Valid name");
}
else {
alert("That's not a name");
}
})
关于javascript - 使用 JQuery 验证 Razor View 中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37706259/