【问题标题】:jQuery UI date picker queryjQuery UI 日期选择器查询
【发布时间】:2021-03-01 17:36:49
【问题描述】:

在 jquery 日期选择器中,有什么方法可以打开我在输入字段中输入的特定年份的日历?就像我输入 2015 一样,2015 日历将打开,默认情况下将选择 1 月 1 日。我添加了以下代码

$( "#datepickerfrom" ).datepicker({
dateFormat: 'yy-mm-dd',
changeYear:true,
changeMonth: true,
yearRange: "2014:+1",
});

但不工作。谁能帮我解决这个问题?

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您可以使用日期选择器的 setDate 选项来设置您想要的任何日期

    $('#datepickerfrom').datepicker("setDate", new Date(2008,9,03) );
    

    【讨论】:

      【解决方案2】:

      您可以使用defaultDate 为日期选择器设置默认日期并计算每次更改的天数差异

      $("#datepickerfrom").datepicker({
          dateFormat: 'yy-mm-dd',
          changeYear: true,
          changeMonth: true,
      });
      
      $("#year").change(function () {
          $('#datepickerfrom').datepicker("option", "defaultDate", parseInt((new Date($(this).val(), 0, 1) - new Date()) / (1000 * 60 * 60 * 24), 10));
          //Or use setDate to fill datepicker field
          //$('#datepickerfrom').datepicker("setDate", new Date($(this).val(), 0, 1));
          
      });
      <link rel="stylesheet" href="//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>
      <input type="number"  min="1970" max="275760" value="2021" id="year">
      <input type="text" id="datepickerfrom">

      【讨论】:

        猜你喜欢
        • 2018-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        相关资源
        最近更新 更多