jQuery 针对自定义 jQueryUI datePickers 验证插件

发布于 2024-08-29 05:48:53 字数 1169 浏览 2 评论 0原文

我有一些日期选择器
我已经自定义了这些,因此它们仅显示月份和年份
这是创建它们的代码

jQuery("input[name*='fechauso']").each(function() {
    jQuery(this).datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        constrainInput: true,
        showOn: 'button',
        buttonText: 'Seleccionar...',

        onClose: function(dateText, inst) { 
            var month = jQuery("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = jQuery("#ui-datepicker-div .ui-datepicker-year :selected").val();
            jQuery(this).datepicker('setDate', new Date(year, month, 1));
        }
    });
});

现在我添加了一个自定义验证器方法(使用插件)来检查这一点:

  • 如果用户没有使用按钮选择日期,则字段为空,因此应该触发自定义验证器方法。这不会发生。

这是自定义验证方法

jQuery.validator.addMethod("isEmpty", function(value, element) {
    return (value == '');
}, "Must select a date with the button besides control");

jQuery("#ct_2_fechauso").rules("add", { 
    required: "#campotilde_psico:checked", 
    isEmpty: true
});

问题是,即使我选择了日期,它总是要求我再次选择日期。 datePicker 字段应该是只读的

I have some datePickers
I've customized these so they show only month and year
This is the code to create them

jQuery("input[name*='fechauso']").each(function() {
    jQuery(this).datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        constrainInput: true,
        showOn: 'button',
        buttonText: 'Seleccionar...',

        onClose: function(dateText, inst) { 
            var month = jQuery("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = jQuery("#ui-datepicker-div .ui-datepicker-year :selected").val();
            jQuery(this).datepicker('setDate', new Date(year, month, 1));
        }
    });
});

Now I've added a custom validator method (using plugin) to check this:

  • If user didn't select a date using the button, field is empty, so the custom validator method should fire. This doesn't happen.

Here is the custom validate method

jQuery.validator.addMethod("isEmpty", function(value, element) {
    return (value == '');
}, "Must select a date with the button besides control");

jQuery("#ct_2_fechauso").rules("add", { 
    required: "#campotilde_psico:checked", 
    isEmpty: true
});

The problem is that even if I select a date, it always ask me to select a date again. datePicker fields should be readonly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-09-05 05:48:53

尝试使用这个

$.datepicker.setDefaults({
    closeOnSelect: true,
    onSelect: function () {
        if (typeof $(this).valid == 'function') {
            $(this).valid();
        }
    }
});

try to use this

$.datepicker.setDefaults({
    closeOnSelect: true,
    onSelect: function () {
        if (typeof $(this).valid == 'function') {
            $(this).valid();
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文