我很难为我的问题找到一个好的解决方案。 任何人都可以告诉我如何在Date的验证事件上显示弹出窗口。我正在使用jQuery验证插件1.6进行验证(jquery.validate.js)。我的情况是,当用户选择日期时,我验证该日期并显示正确的错误消息,如果无效。如果日期为无效,我想要一个密码的弹出窗口,使无效日期有效。 不幸的是,我找不到任何好的解决方案。
这是我的代码:
$(document).ready(function () { ... $('#txtDate').rules("add", { required: true, restrictDate: true, messages: { required: "Please enter log date", restrictDate: "Check the selected date." } }); ... $.validator.addMethod("restrictDate", function (value, element) { var activityDate = GetDate($('#txtDate').val(), formatOfDate); if (Date.parse(activityDate) > Date.parse(dt)) { return false; } else return true; }, "Date comparison error." ); ... //Registering date picker $('#txtDate').datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true, constrainInput: true });
... ....
... ....
答案 0 :(得分:0)
你见过dialog component of jQuery UI吗?您可以使用任何所需的消息显示任意模态对话框(弹出窗口)。
使用jQuery validate插件,在调用validate时使用invalidHandler
,如下所示:
$(".selector").validate({
invalidHandler: function(event, validator) {
// If there are any errors in your form, control reaches this function
// First check if your date field was the source of the error
if(validator.element('#datefield')){
// If it was, show the dialog box you want.
$('#yourdiv').html("<div> Any html message/content you want here</div>");
$('#yourdiv').dialog();
}
}
})
答案 1 :(得分:0)
if(!$('#dateId').valid()){
$('#divId').html("Error message");
$( "#divId" ).dialog();
}