【问题标题】:jQuery validate plugin against custom jQueryUI datePickers针对自定义 jQueryUI datePickers 的 jQuery 验证插件
【发布时间】:2010-04-15 21:02:17
【问题描述】:

我有一些日期选择器
我已经定制了这些,所以它们只显示月份和年份
这是创建它们的代码

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 字段应该是只读的

【问题讨论】:

    标签: jquery methods datepicker jquery-validate


    【解决方案1】:

    尝试使用这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多