Jquery UI Datepicker - 选择时间段时显示天数
Jquery UI Datepicker - show the number of days when choosing a period
到select一个日期范围,我使用JqueryUI日期选择器插件。我使用的代码应该在选择时间段时显示天数。
要连接插件,我使用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)
}
不幸的是,出于某种原因,此代码在选择期间时不显示天数。例如,有必要显示 "You have chosen - 5 days."
如何修复代码以便一切正常?需要你的帮助!谢谢!
#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>
到select一个日期范围,我使用JqueryUI日期选择器插件。我使用的代码应该在选择时间段时显示天数。
要连接插件,我使用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)
}
不幸的是,出于某种原因,此代码在选择期间时不显示天数。例如,有必要显示 "You have chosen - 5 days."
如何修复代码以便一切正常?需要你的帮助!谢谢!
#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>