问题描述
我有一个复选框,您可以在其中隐藏/显示日期选择器字段.
I have a checkbox where you can hide/show a datepicker field.
但是当datepicker字段隐藏时,不必进行验证.
But when the datepicker field is hidden there has to be no validation.
这是复选框:
@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" }))
此字段的属性如下所示:
The properties of this fields are looking like this:
[Display(Name = "Einddatum wijziging")]
[Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "Global_Validatie_VeldVerplicht")]
public DateTime? EindDatum { get; set; }
public bool showEindDatum { get; set; }
这是它的jQuery:
And this is the jquery of it:
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字段时没有验证?
So what I have to change that there is no validation when the datepicker field is hidden?
谢谢
所以你的意思是
$.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方法,例如:
But the form has also a post method, like this:
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 display:none的元素以及具有不可见父元素的任何元素(使用相同条件)
By default jQuery validate ignores hidden fields, elements with zero width and height, those with css display:none and any elements with an invisible parent (using same criteria).
不过,可以通过添加以下脚本来覆盖忽略设置
The ignore setting can however be overridden by adding the following script
// By default validator ignores hidden fields.
// change the setting here to ignore nothing
$.validator.setDefaults({ ignore: null });
这篇关于仅当在ASP.NET MVC中可见字段时,才需要进行字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!