jQuery datepicker,禁用月份选择

时间:2010-08-17 11:20:33

标签: jquery jquery-ui-datepicker

嗨,我确定必须有一个简单的方法来做到这一点,但找不到将它设置在任何地方的选项。

我正在尝试禁用允许用户更改月份的左/右按钮。我删除了几个月的下拉列表,但无法摆脱按钮。

    $("#date").datepicker({
    changeMonth: false,
    changeYear: false,
    dateFormat: 'dd/mm/yy',
    duration: 'fast'
});

4 个答案:

答案 0 :(得分:13)

您可以使用stepMonths有效地停用它们,方法是点击它们时无处可去,如下所示:

$("#date").datepicker({
  changeMonth: false,
  changeYear: false,
  dateFormat: 'dd/mm/yy',
  duration: 'fast',
  stepMonths: 0
});

You can give it a try here

或者,你可以删除这样的按钮:

$("#date").datepicker({
  changeMonth: false,
  changeYear: false,
  dateFormat: 'dd/mm/yy',
  duration: 'fast'
}).focus(function() {
  $(".ui-datepicker-prev, .ui-datepicker-next").remove();
});​

You can give that a try here,这是有效的,因为默认的showOn选项是focus,如果你使用的是另一个事件,只需在之后绑定到 { {1}}调用(因此它的事件首先运行,你无法隐藏尚未创建的事件)。

答案 1 :(得分:3)

上述解决方案对我不起作用,我开发了这个:

$('#datepicker').find('.ui-datepicker-next').remove();
$('#datepicker').find('.ui-datepicker-prev').remove();

答案 2 :(得分:2)

这是我使用datepicker选择年份的方法:

if (document.styleSheets[0].addRule) {
   document.styleSheets[0].addRule(".ui-datepicker-calendar", "display: none;");
else {
   document.styleSheets[0].insertRule(".ui-datepicker-calendar {display: none;}", 0);
}

 .datepicker({    
                  dateFormat: 'yy',
                  showMonthAfterYear: false,
                  changeMonth: false,
                  changeYear: true,
                  showButtonPanel: true,
                  stepMonths: 12,
                  monthNames: ["", "", "", "", "", "", "", "", "", "", "", "", "", ""],
                  onChangeMonthYear: function (year) {
                      $(this).val(year);
                  }
             });

答案 3 :(得分:0)

我不知道如何在jQuery中直接执行它(或者如果可能的话)。

但如果你在那里找不到办法,你可以随时隐藏css中的按钮。

你想要的css选择器是(从内存中你可能需要检查)

.ui-datepicker .ui-datepicker-prev, 
.ui-datepicker .ui-datepicker-next
{
    display:none;
}