日期选择器最大日期限制

时间:2019-05-13 09:31:17

标签: javascript jquery jquery-ui-datepicker

我有以下代码,并且仅在maxDate时才想使用selector === '#Birth'。 我该如何实现?

function customdatepicker(selector) {
    $(selector).datepicker({
        inline: true,
        showOtherMonths: true,
        dateFormat: 'dd/mm/yy',
        prevText: '<',
        nextText: '>',
        maxDate: 0
    });
}

1 个答案:

答案 0 :(得分:0)

初始化后,可以使用optiondatepicker('option', ...))方法来更新选择器的最大日期。

customdatepicker('#test');
customdatepicker('#Birth');

function customdatepicker(selector) {
  const picker = $(selector).datepicker({
    inline: true,
    showOtherMonths: true,
    dateFormat: 'dd/mm/yy',
    prevText: '<',
    nextText: '>'
  });
  
  if (selector === '#Birth') {
    // Impossible to choose a date after the 20th of May 2019.
    picker.datepicker('option', 'maxDate', '20/05/2019');
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
  src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
  integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
  crossorigin="anonymous"></script>
  
<label for="test">Date: <input type="text" id="test"></label>
<label for="Birth">Birth: <input type="text" id="Birth"></label>