【问题标题】:jQuery UI Date Picker - Range change by clicking another buttonjQuery UI 日期选择器 - 通过单击另一个按钮更改范围
【发布时间】:2019-05-23 02:31:44
【问题描述】:

这是我当前的代码。它是 jquery ui 日期选择器,设置了默认的开始日期和结束日期。默认开始日期和结束日期有效。我想要的是当我单击#mybutton 时,我希望我的默认开始和结束日期更改为新的开始和结束日期。所以我在#mybutton的点击函数中设置了新变量。但它仍然是默认的开始和结束日期。

我想我的问题是由于 javascript 全局和局部变量范围。所以我尝试将变量放在所有地方(函数内部或外部)。但仍然没有得到新的开始日期和结束日期。

请给个建议?

$(document).ready(function() {
    var minDate = "default start date";
    var maxDate = "default end date";

    $("#myDate").datepicker({
        minDate: minDate,
        maxDate: maxDate,
    });

    $("#mybutton").click(function() {
        var minDate = "new start date";
        var maxDate = "new end date";
    });
});

【问题讨论】:

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


    【解决方案1】:

    您可以使用以下代码来更新日期选择器的选项:

      $("#myDate").datepicker("option", {
        minDate: newMinDate,
        maxDate: newMaxDate
      });
    

    下面的演示展示了它的实际应用。我还添加了一个函数,用于检查当前选择的日期是否在新的日期范围之外,如果是,则清除该值。 jquerui datepicker的默认动作是将选中的日期移动到新范围内最近的日期。

    您可以使用复选框并重置演示以查看不同的行为。

    如果您还需要其他东西,请告诉我。

    // Setup datepicker on page load
    var minDate = new Date(2018, 11, 8);
    var maxDate = new Date(2018, 11, 24);
    $("#myDate").datepicker({
      minDate: minDate,
      maxDate: maxDate,
    });
    
    
    // Add click event to button
    $("#myButton").click(function() {
    
      // Create new dates
      var newMinDate = new Date(2018, 11, 10);
      var newMaxDate = new Date(2018, 11, 16);
    
      // Check if selected date is outside of range
      // Comment this out if you want the date just to be changed to within the new date range
      checkSelectedDate(newMinDate, newMaxDate);
    
      // Update options for datepicker
      $("#myDate").datepicker("option", {
        minDate: newMinDate,
        maxDate: newMaxDate
      });
    
    });
    
    
    function checkSelectedDate(newMinDate, newMaxDate) {
    
      // Exit if checkbox is not checked
      // Only needed for demo purposes
      if ($("#clearDate").prop("checked") == false) {
        return
      }
    
      // Get current date
      var selectedDate = new Date;
      selectedDate = $("#myDate").datepicker("getDate");
    
      // Check if it is outside the new range of dates
      if ((selectedDate < newMinDate) || (selectedDate > newMaxDate)) {
    
        // Clear date as outside of range
        $("#myDate").datepicker('setDate', "");
    
      }
    
    }
    
    
    // Reset date range to restart demo
    $("#reset").click(function() {
     
      var minDate = new Date(2018, 11, 8);
      var maxDate = new Date(2018, 11, 24);
    
      $("#myDate").datepicker("option", {
        minDate: minDate,
        maxDate: maxDate
      });
    
    });
    $("#myDate").datepicker( {
      minDate: minDate, maxDate: maxDate,
    }
    
    );
    <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.0/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css">
    
    <div>
    
      <input id="myDate">
    
      <button id="myButton">Change Date Range</button>
    
    </div>
    
    <hr>
    
    <div>
    
      <button id="reset">Reset Demo</button>
    
      <input type="checkbox" id="clearDate" checked>Clear date
    
    </div>

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多