Jquery 仅当字段在asp.net mvc中可见时才需要进行字段验证
我有一个复选框,您可以在其中隐藏/显示日期选择器字段 但是当datepicker字段被隐藏时,就不必进行验证 这是复选框:Jquery 仅当字段在asp.net mvc中可见时才需要进行字段验证,jquery,asp.net-mvc,kendo-ui,datepicker,jquery-validate,Jquery,Asp.net Mvc,Kendo Ui,Datepicker,Jquery Validate,我有一个复选框,您可以在其中隐藏/显示日期选择器字段 但是当datepicker字段被隐藏时,就不必进行验证 这是复选框: @Html.CheckBox("showEindDatum", (Request.Form["showEindDatum"] ?? string.Empty).Contains("true")) 这是日期选择器: @FormGroupHelper.CreateFormGroup(Html, m => m.EindDatum, Html.Kendo().Da
@Html.CheckBox("showEindDatum", (Request.Form["showEindDatum"] ?? string.Empty).Contains("true"))
这是日期选择器:
@FormGroupHelper.CreateFormGroup(Html, m => m.EindDatum, Html.Kendo().DatePickerFor(m => m.EindDatum).Min(Model.EindDatum.HasValue ? Model.EindDatum.Value : DateTime.Today).Format("dd-MM-yyyy").ParseFormats(new string[] { "ddMMyyyy" }))
此字段的属性如下所示:
[Display(Name = "Einddatum wijziging")]
[Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "Global_Validatie_VeldVerplicht")]
public DateTime? EindDatum { get; set; }
public bool showEindDatum { get; set; }
using (Html.BeginForm("Formulier", "PersoneelsDossier", FormMethod.Post, new { @id = "PDForm", name = "PDForm" }))
}
$(document).ready(function () {
//$("#PDForm").data("validator").settings.ignore = ".ignore, :hidden";
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
$.validator.setDefaults({ ignore: null });
});
$.validator.setDefaults({ ignore: null });
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
这是它的jquery:
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
}
$("#showEindDatum").on("click", function ()
{
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
});
那么我要改变什么,当datepicker字段被隐藏时,没有验证
多谢各位
你是说:
$.validator.setDefaults({ ignore: null });
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
这不管用
但是表单还有一个post方法,如下所示:
[Display(Name = "Einddatum wijziging")]
[Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "Global_Validatie_VeldVerplicht")]
public DateTime? EindDatum { get; set; }
public bool showEindDatum { get; set; }
using (Html.BeginForm("Formulier", "PersoneelsDossier", FormMethod.Post, new { @id = "PDForm", name = "PDForm" }))
}
$(document).ready(function () {
//$("#PDForm").data("validator").settings.ignore = ".ignore, :hidden";
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
$.validator.setDefaults({ ignore: null });
});
$.validator.setDefaults({ ignore: null });
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
你的意思是这样的:
[Display(Name = "Einddatum wijziging")]
[Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "Global_Validatie_VeldVerplicht")]
public DateTime? EindDatum { get; set; }
public bool showEindDatum { get; set; }
using (Html.BeginForm("Formulier", "PersoneelsDossier", FormMethod.Post, new { @id = "PDForm", name = "PDForm" }))
}
$(document).ready(function () {
//$("#PDForm").data("validator").settings.ignore = ".ignore, :hidden";
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
$.validator.setDefaults({ ignore: null });
});
$.validator.setDefaults({ ignore: null });
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
你的意思是这样的:
[Display(Name = "Einddatum wijziging")]
[Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "Global_Validatie_VeldVerplicht")]
public DateTime? EindDatum { get; set; }
public bool showEindDatum { get; set; }
using (Html.BeginForm("Formulier", "PersoneelsDossier", FormMethod.Post, new { @id = "PDForm", name = "PDForm" }))
}
$(document).ready(function () {
//$("#PDForm").data("validator").settings.ignore = ".ignore, :hidden";
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
$.validator.setDefaults({ ignore: null });
});
$.validator.setDefaults({ ignore: null });
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
不起作用默认情况下,jQuery validate忽略隐藏字段、宽度和高度为零的元素、具有css显示的元素:无以及具有不可见父元素的任何元素(使用相同的条件) 但是,可以通过添加以下脚本来覆盖忽略设置
// By default validator ignores hidden fields.
// change the setting here to ignore nothing
$.validator.setDefaults({ ignore: null });
非常感谢。但这不起作用。我更新了帖子。@thecodeEngine在您的验证运行之前放了什么?你是什么意思?Jquery位于表单字段下。是。@代码引擎需要立即将其置于jquery验证下load@thecodeEngine尝试将第一条语句放在document ready函数中使用条件验证属性,例如
[RequiredIf]
属性(或要了解如何编写自己的语句,请参阅