如何在 jQuery 日期选择器中禁用月份选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3501808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I disable month selection in the jQuery datepicker?
提问by JBoom
I am trying disable the left/right buttons that allow the user to change months. I've removed the drop down list of months but can't get rid of the buttons.
我正在尝试禁用允许用户更改月份的左/右按钮。我已经删除了月份的下拉列表,但无法删除按钮。
$("#date").datepicker({
changeMonth: false,
changeYear: false,
dateFormat: 'dd/mm/yy',
duration: 'fast'
});
回答by Nick Craver
You can effectively disable them using stepMonths
by making them go nowhere when clicked, like this:
您可以stepMonths
通过使它们在单击时无处可去来有效地禁用它们,如下所示:
$("#date").datepicker({
changeMonth: false,
changeYear: false,
dateFormat: 'dd/mm/yy',
duration: 'fast',
stepMonths: 0
});
Or, you could remove the buttons like this:
或者,您可以删除这样的按钮:
$("#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, this works because the default showOn
option is focus
, if you're using a different event, just bind to that afterthe .datepicker()
call (so its event runs first, you can't hide what isn't created yet).
您可以在这里尝试一下,这是因为默认showOn
选项是focus
,如果您使用不同的事件,只需在调用后绑定到.datepicker()
该事件(因此它的事件首先运行,您无法隐藏尚未创建的内容) )。
回答by nimrod
The mentioned solution did not work for me, I developped this:
提到的解决方案对我不起作用,我开发了这个:
$('#datepicker').find('.ui-datepicker-next').remove();
$('#datepicker').find('.ui-datepicker-prev').remove();
回答by Pa0l0
This is my way to make datepicker pick only year:
这是我让 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);
}
});
回答by fearofawhackplanet
I don't know how to do it directly in jQuery (or if it's possible).
我不知道如何直接在 jQuery 中完成(或者如果可能的话)。
But if you can't find a way to do it in there, you could always just hide the buttons in css.
但是如果你在那里找不到方法,你总是可以在 css 中隐藏按钮。
The css selectors you want are (from memory so you might need to check)
您想要的 css 选择器是(来自内存,因此您可能需要检查)
.ui-datepicker .ui-datepicker-prev,
.ui-datepicker .ui-datepicker-next
{
display:none;
}