【问题标题】:Disable previous dates of a datepicker according to selected date in javascript or jquery根据javascript或jquery中的选定日期禁用日期选择器的先前日期
【发布时间】:2015-07-03 10:17:04
【问题描述】:

我有两个日期选择器,例如 startdate 和 enddate。如果我在 startdate 中选择 2015 年 6 月 18 日,我必须在 enddate 中禁用 datepicker 中的先前日期到 18thjune2015。

这是我的代码。

 $("#txtstartdate").datepicker({ minDate: 0 });

结束日期:

    var startdate = $("#txtstartdate").val();
    $("#txtenddate").datepicker({ minDate: startdate }); 

它不工作。谁能帮我解决这个问题。 谢谢。

【问题讨论】:

    标签: javascript jquery jquery-ui datepicker


    【解决方案1】:

    您需要使用更改开始日期的选项方法来设置它

    $("#txtstartdate").datepicker({
      minDate: 0,
      onSelect: function(date) {
        $("#txtenddate").datepicker('option', 'minDate', date);
      }
    });
    
    $("#txtenddate").datepicker({});
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/redmond/jquery-ui.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>
    
    <input id="txtstartdate" />
    <input id="txtenddate" />

    【讨论】:

    • 它的工作..谢谢..:)...但是第一次它工作..第二次它的所有日期都被禁用,除了本月的最后一天。
    【解决方案2】:

    如果你使用带有属性type="datetime-local" 的输入标签,试试这个:

    let today = new Date().toLocaleString('sv-SE').slice(0, 16).replace(' ', 'T');
    document.getElementById('someId').setAttribute("min", today);
    

    【讨论】:

      【解决方案3】:

      尝试添加yearRange:

      $("#txtenddate").datepicker({ 
          minDate: startdate,
          yearRange: '1999:2020', 
      }); 
      

      【讨论】:

        【解决方案4】:

        你必须使用option

        插入

        $("#txtenddate").datepicker({ minDate: startdate }); 
        

        这样做:

        $("#txtenddate").datepicker('option', 'minDate', date);
        

        Change mindate in datepicker

        【讨论】:

          【解决方案5】:
          $("#txtstartdate").datepicker({
            minDate: 0,
            onSelect: function(date) {
              $("#txtenddate").datepicker('option', 'minDate', date);
            }
          });
          
          $("#txtenddate").datepicker({});
          
          
          
          <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
          <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/redmond/jquery-ui.css">
          <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>
          
          <input id="txtstartdate" />
          <input id="txtenddate" />
          

          【讨论】:

          • 请描述您的问题
          【解决方案6】:

          如果 minDate 不起作用,您可以尝试 startDate 它肯定会起作用。

          $('#datepicker4').datepicker({
            autoclose: true,
            startDate: new Date() 
           });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-03-16
            • 2020-08-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-17
            • 1970-01-01
            • 2014-11-25
            相关资源
            最近更新 更多