我是如何在新的动态html中使用jquery验证的?
我通过jquery模式调用一个视图(部分视图),在该模式中,我有一个按钮来执行创建新员工的操作。但是,我没有设法使,如果空间没有填充或填充不好,错误警告会出现,因为它发生在正常的看法 有人能帮我吗 我的视图索引(员工)我是如何在新的动态html中使用jquery验证的?,jquery,asp.net-mvc,jquery-validate,Jquery,Asp.net Mvc,Jquery Validate,我通过jquery模式调用一个视图(部分视图),在该模式中,我有一个按钮来执行创建新员工的操作。但是,我没有设法使,如果空间没有填充或填充不好,错误警告会出现,因为它发生在正常的看法 有人能帮我吗 我的视图索引(员工) &时代; 克里亚尔功能酒店 叫喊 粪便 @节脚本{ <script> $(document).ready(function () { <button type="button" class="btn
&时代;
克里亚尔功能酒店
叫喊
粪便
@节脚本{
<script>
$(document).ready(function () {
<button type="button" class="btn btn-info btn-lg btn-create" data-toggle="modal" data-target="#myModal3">Criar Funcionário</button>
$(".btn-create").on("click", function (e) {
$("#create").load("/Employees/Create/");
var validator = $('#create').validate();
validator.form();
});
$(".btn-submitcreate").on("click", function (e) {
var parentDiv = $('#create');
$.post(parentDiv.find('form').attr('action'),
parentDiv.find('form').serialize(),
function (data) {
location.reload();
});
});
});
</script>
$(文档).ready(函数(){
克里亚尔功能酒店
$(“.btn创建”)。在(“单击”上,函数(e){
$(“#创建”).load(“/Employees/create/”);
var validator=$('#create').validate();
form();
});
$(.btn submitcreate”)。在(“单击”上,函数(e){
var parentDiv=$('#create');
$.post(parentDiv.find('form').attr('action'),
parentDiv.find('form').serialize(),
功能(数据){
location.reload();
});
});
});
}
我的创建视图
以下代码是“创建”视图中的“我的字段”
<div class="col-md-2">
<div class="col-md-12">
@Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-10">
<div class="col-md-12">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="col-md-12">
@Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
@Html.LabelFor(model => model.MaritalStatus, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.MaritalStatus, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MaritalStatus, "", new { @class = "text-danger" })
</div>
</div>
</div>
@LabelFor(model=>model.Number,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.Number,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Number,“,new{@class=“text danger”})
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
@LabelFor(model=>model.BirthDate,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.BirthDate,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.BirthDate,“,new{@class=“text danger”})
@LabelFor(model=>model.MaritalStatus,htmlAttributes:new{@class=“controllabel”})
@EditorFor(model=>model.MaritalStatus,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.MaritalStatus,“,new{@class=“text danger”})
最简单的方法是在加载DOM时,而不是在单击submit按钮时,在表单上调用validate()
,然后将验证规则作为data
属性附加到DOM中的动态元素。另外,使用validate
的submithandler
属性使用AJAX发送表单。我在加载表单时输入了验证代码,我仍然不明白如何将验证规则作为数据属性应用于动态元素,并使用validate submithandler属性使用AJAX提交表单。“我还是不明白我是怎么。。。使用validate submithandler属性使用AJAX提交表单”-您是否查看了有关此插件的文档、代码示例或其他问题?
<div class="col-md-2">
<div class="col-md-12">
@Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-10">
<div class="col-md-12">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="col-md-12">
@Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
@Html.LabelFor(model => model.MaritalStatus, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.MaritalStatus, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MaritalStatus, "", new { @class = "text-danger" })
</div>
</div>
</div>