选择日期后,如何在jQuery UI日期戳记中触发验证? [英] How can I trigger validation to occur on jQuery UI datepicker once a date has been selected?
问题描述
我有一些客户端验证,检查确保 EndDate
大于或等于 StartDate
。验证工作,但它不会像我想要的那样触发。一旦在 EndDate
的datepicker上选择日期,我希望它开始。我该如何做到这一点?我尝试了以下内容:
I have some client-side validation that checks to ensure that the EndDate
is greater than or equal to StartDate
. The validation works, but it's not firing as I would like it to. I would like it to fire as soon as a date is selected on the datepicker for EndDate
. How can I accomplish this? I've tried the following:
Datepicker代码:
Datepicker code:
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
onClose: function () {
$(this).focusout();
}
});
验证码:
$("#EndDate").focusout(function () {
jQuery.validator.addMethod('datetimegreaterthanorequal', function (value, element, params) {
var startDateValue = $(params.element).val();
return Date.parse(value) >= Date.parse(startDateValue);
}, '');
jQuery.validator.unobtrusive.adapters.add('datetimegreaterthanorequal', ['startdate'], function (options) {
var prefix = options.element.name.substr(0, options.element.name.lastIndexOf('.') + 1),
other = options.params.startdate,
fullOtherName = appendModelPrefix(other, prefix),
element = $(options.form).find(':input[name=' + fullOtherName + ']')[0];
options.rules['datetimegreaterthanorequal'] = {
element: element
};
if (options.message) {
options.messages['datetimegreaterthanorequal'] = options.message;
}
});
function appendModelPrefix(value, prefix) {
if (value.indexOf('*.') === 0) {
value = value.replace('*.', prefix);
}
return value;
}
})
推荐答案
您在我们的 .focusout()
事件中分配验证器。
You are assigning the validators inside of our .focusout()
event. Remove that block completely as you want them assigned only once.
您可以轻松地触发 onSelect
选项的验证,例如以下:
You can easily trigger validation on the onSelect
option like the following:
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
onSelect: function () {
$("#myForm").valid();
}
});
这篇关于选择日期后,如何在jQuery UI日期戳记中触发验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!