jQuery UI Datepicker-显示选择期间的天数

时间:2019-03-31 15:30:37

标签: javascript jquery datepicker jquery-ui-datepicker

要选择日期范围,请使用Jquery UI Datepicker插件。我使用的代码应显示选择期间的天数。

要连接插件,请使用CDN:

<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css"></link>
<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>

HTML代码:

<form>
    <label>From</label>
    <input id="from" readonly="readonly" name="from" type="text" value="">
    <label>To</label>
    <input id="to" readonly="readonly" name="to" type="text" value="">
    <label>You choosed</label>
    <input id="days" readonly="readonly" name="days" type="text" value=""> days.
</form>

插件初始化:

var from = new Date();
var to = new Date();
var dayDiff = 1;

$(function() {
    var dates = $("#from, #to").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 1,
            onSelect: function(selectedDate) {
                    var option = this.id == "from" ? "minDate" : "maxDate",
                            instance = $(this).data("datepicker"),
                            date = $.datepicker.parseDate(
                                    instance.settings.dateFormat ||
                                    $.datepicker._defaults.dateFormat,
                                    selectedDate, instance.settings);
                    dates.not(this).datepicker("option", option, date);

                    if (this.id == "from") {
                            from = $(this).datepicker('getDate');
                            if (!(to == "")) {
                                    update_days()
                            }
                    }
                    if (this.id == "to") {
                            to = $(this).datepicker('getDate');
                            update_days()
                    }
            }
    });
});

function update_days() {
    dayDiff = Math.ceil((to - from) / (1000 * 60 * 60 * 24));
    $("#days").empty()
    $("#days").append(dayDiff)
}

不幸的是,由于某些原因,此代码未显示选择期间的天数。例如,有必要显示“您已选择-5天”。

如何修复代码以便一切正常?需要你的帮助!谢谢!

1 个答案:

答案 0 :(得分:1)

#days是一个input元素,它没有任何HTML内容附加,不会对值进行任何更改。要使其工作,请使用val()方法。

function update_days() {
    dayDiff = Math.ceil((to - from) / (1000 * 60 * 60 * 24));
    $("#days").val(dayDiff)
}

var from = new Date();
var to = new Date();
var dayDiff = 1;

$(function() {
  var dates = $("#from, #to").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 1,
    onSelect: function(selectedDate) {
      var option = this.id == "from" ? "minDate" : "maxDate",
        instance = $(this).data("datepicker"),
        date = $.datepicker.parseDate(
          instance.settings.dateFormat ||
          $.datepicker._defaults.dateFormat,
          selectedDate, instance.settings);
      dates.not(this).datepicker("option", option, date);

      if (this.id == "from") {
        from = $(this).datepicker('getDate');
        if (!(to == "")) {
          update_days()
        }
      }
      if (this.id == "to") {
        to = $(this).datepicker('getDate');
        update_days()
      }
    }
  });
});

function update_days() {
  dayDiff = Math.ceil((to - from) / (1000 * 60 * 60 * 24));

  $("#days").val(dayDiff)
}
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css"></link>
<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>

<form>
  <label>From</label>
  <input id="from" readonly="readonly" name="from" type="text" value="">
  <label>To</label>
  <input id="to" readonly="readonly" name="to" type="text" value="">
  <label>You choosed</label>
  <input id="days" readonly="readonly" name="days" type="text" value=""> days.
</form>