我有一个简单的MVC3表单,其中启用了日期字段和客户端验证(jquery.validate / jquery.validate.unobtrusive)。我添加了代码,将数据报警器(jQuery UI)附加到文档的date字段中。但是,如果在单击submit按钮之前单击的最后一次单击datepicker,而日期字段无效,则会导致该字段的datepicker自动显示自己。我不想要这个。我怎么才能停用?
编辑:
在检查了验证插件的代码之后,它似乎试图使用下面的focusInvalid()函数手动地关注最后一个活动输入控件:
focusInvalid: function() {
if( this.settings.focusInvalid ) {
try {
$(this.findLastActive() || this.errorList.length && this.errorList[0].element || [])
.filter(":visible")
.focus()
// manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
.trigger("focusin");
} catch(e) {
// ignore IE throwing errors when focusing hidden elements
}
}
},
处理这一问题似乎有两种选择。一种是将验证器本身上的focusInvalid设置设置为false。我选择了猴子补丁focusInvalid函数,因为它允许我关注表单中的第一个无效元素,而不一定是最后一个活动元素。
$('form').data('validator').focusInvalid = function () {
$(this.currentForm).find('.input-validation-error').first().focus();
};
不过,我很想听听其他解决这个问题的方法。
发布于 2012-03-07 16:34:00
如果在您的情况下可以接受的话,您可以使用下面的datepicker选项使用一个按钮来显示datepicker:
showOn: 'button', showButtonPanel: true, buttonImage: '<your image file>',
buttonImageOnly: true,
buttonText: 'Choose a Date',
如果您这样做,数据报警器将不会自动显示,以防验证在提交表单时失败。
https://stackoverflow.com/questions/8931357
复制相似问题