禁用JQueryUI DatePicker的年份

时间:2018-12-06 20:47:14

标签: jquery-ui datepicker jquery-ui-datepicker

我正在尝试从“ YearPicker”禁用年份,这是一个DatePicker,您只能选择年份。我需要禁用所有年份并仅启用范围的几年(例如,从2012年到现在)。

我正在使用JQueryUI DatePicker,它看起来像这样:

enter image description here

CSS:

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
   <style>
        .ui-datepicker-calendar {
            display: none;
        }        
    </style>

JS:

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
    $( function() {
        $('#datepicker').datepicker({
            viewMode: 2,
            format: 'yyyy',
            maxDate: new Date(new Date().getFullYear(), 11, 31),
            minDate: new Date(2012, 0, 1)
        });
    })
</script>

HTML:

<label>Year: <input class="form-control" type="text" id="datepicker"></label>

2 个答案:

答案 0 :(得分:0)

您可以执行自己的工作,但我认为这比需要的工作要多。我建议也许考虑改用自动完成。看看:

ln_is = [d['url'] for d in generator if d['url_source'] is '"http://ln.is']
$(function() {
  $("#datepicker").autocomplete({
    minLength: 0,
    source: function(req, resp) {
      var today = new Date();
      var min = today.getFullYear() - 6;
      // Adjust above as needed, currently 6 years before this year
      var results = [];
      for (min; min <= today.getFullYear(); min++) {
        results.push(min.toString());
      }
      resp(results)
    }
  }).focus(function(e) {
    $(e.target).autocomplete("search", "");
  });
})

如果需要,也可以将其移至其自身的功能。

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<label>Year: <input class="form-control" type="text" id="datepicker" data-years="6"></label>

您还可以调整主题以将其水平列出,而不是垂直列出。

希望有帮助。

答案 1 :(得分:0)

解决了!我正在导入Bootstrap DatePicker和JQueryUI DatePicker,我以为我只在使用第二个,但是应用程序仅检测到第一个,所以我的代码现在可以正常工作:

$('#datepicker').datepicker({
                    minViewMode: 2,
                    format: 'yyyy',
                    startDate: '2012',
                    endDate: 'y'
                });

感谢大家的帮助!

相关问题