获得';无法读取属性';设置';未定义的';在Jquery中使用addMethod添加新规则时出错
我试图在弹出窗口中显示视图以添加数据。在弹出窗口中显示视图之前,我试图在字段上添加jquery验证。但它给我的错误是“无法读取未定义的”属性“设置”。下面是一些代码 这是要在另一页的弹出窗口中显示的视图获得';无法读取属性';设置';未定义的';在Jquery中使用addMethod添加新规则时出错,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我试图在弹出窗口中显示视图以添加数据。在弹出窗口中显示视图之前,我试图在字段上添加jquery验证。但它给我的错误是“无法读取未定义的”属性“设置”。下面是一些代码 这是要在另一页的弹出窗口中显示的视图 @model MVCGridAjax.Models.DomainMaster @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form1" })) { @Html.AntiForgeryToken() <
@model MVCGridAjax.Models.DomainMaster
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form1" }))
{
@Html.AntiForgeryToken()
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Player</h4>
</div>
<div class="modal-body">
<form>
<input type="hidden" id="playerId" />
<div class="form-group">
@Html.LabelFor(model => model.DomainName)
@Html.TextBoxFor(model => model.DomainName, new { @class = "form-control", placeholder = "Enter Name" })
@Html.ValidationMessageFor(model => model.DomainName)
</div>
<div class="form-group">
@Html.LabelFor(model => model.DomainDescription)
@Html.TextBoxFor(model => model.DomainDescription, new { @class = "form-control", placeholder = "Enter Description" })
@Html.ValidationMessageFor(model => model.DomainDescription)
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="btnSave" class="btn btn-primary" onclick="Save()">Save</button>
</div>
</div>
</div>
@model MVCGridAjax.Models.DomainMaster
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“form1”}))
{
@Html.AntiForgeryToken()
&时代;
玩家
@LabelFor(model=>model.DomainName)
@Html.TextBoxFor(model=>model.DomainName,新的{@class=“form control”,placeholder=“Enter Name”})
@Html.ValidationMessageFor(model=>model.DomainName)
@LabelFor(model=>model.DomainDescription)
@TextBoxFor(model=>model.DomainDescription,新的{@class=“form control”,placeholder=“Enter Description”})
@Html.ValidationMessageFor(model=>model.DomainDescription)
接近
拯救
}
jq(函数()
{
jQuery.validator.addMethod(“domainNameRequired”,函数(值,元素)
{
如果(值==未定义| |值==“”)
返回false;
其他的
返回true;
})
jq(“域名”)。规则(“添加”、“域名必填”);
jq(“表格1”)。删除数据(“验证人”);
jq(“表格1”)。删除数据(“不引人注目的验证”);
jQuery.validator.unobtrusive.parse(“form1”);
});
和在弹出页面中
索引
var jq=$.noConflict();
函数Add(){
jq(“#playerModal”).load('@Url.Action(“GetDomainVeiw”))
jq(“playerModal”).modal(“show”);
}
功能编辑(e){
jq(“#playerModal”).load('@Url.Action(“getdomainview”)'+“/”+e.data.id)
jq(“playerModal”).modal(“show”);
}
函数Save()
{
if(jq(“#form1”).valid())
{
警报(“有效”);
}
其他的
{
警报(“无效”);
}
}
jq(文档).ready(函数(){
grid=jq(“#grid”).grid({
数据键:“Id”,
uiLibrary:“引导程序”,
栏目:[
{字段:“Id”,宽度:50,可排序:true},
{字段:“域名”,标题:“域名”,可排序:true},
{字段:“域描述”,标题:“域描述”,可排序:true},
{标题:,字段:“编辑”,宽度:34,类型:“图标”,图标:“字形图标铅笔”,工具提示:“编辑”,事件:{“单击”:编辑}/,,
//{标题:,字段:“删除”,宽度:34,类型:“图标”,图标:“字形图标删除”,工具提示:“删除”,事件:{“单击”:删除}
],
寻呼机:{启用:true,限制:5,大小:[2,5,10,20]}
});
$(“#btnAddPlayer”)。在(“单击”,添加);
});
添加新域
<table id="grid" data-source="@Url.Action("GetDomains")"></table>
<div class="modal fade" id="playerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
</div>
我得到了答案,那就是
jQuery.validator.classRuleSettings.domainName = { domainNameRequired: true }
但是上面的答案只适用于类名。有没有其他方法不使用类就可以做同样的事情?查看浏览器中的开发者控制台,获取JS错误的实际来源。你在这里发布的任何内容都没有提到“设置”,所以如果没有导致问题的特定代码,你怎么能指望任何人来帮助你呢?我发现问题出在哪里了。问题是我们在向特定元素添加规则的地方。jq(函数(){jQuery.validator.addMethod(“domainNameRequired”,函数(value,element){if(value==undefined | | value==”)返回false;else返回true;})jq(“DomainName”)。规则(“add”,“domainNameRequired”);//这才是真正的问题;jQuery.validator.classRuleSettings.domainName={domainNameRequired:true}是解决方案。但它只对类名(这里的domainName)起作用。如何使用元素名来实现。
<table id="grid" data-source="@Url.Action("GetDomains")"></table>
<div class="modal fade" id="playerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
</div>
jQuery.validator.classRuleSettings.domainName = { domainNameRequired: true }