[英]Jquery UI Datepicker - show the number of days when choosing a period
To select a date range, I use the Jquery UI Datepicker plugin.为了选择日期范围,我使用了 Jquery UI Datepicker 插件。 I use a code that should show the number of days when choosing a period.
我使用的代码应该在选择一个时期时显示天数。
To connect the plugin, I use CDN:要连接插件,我使用 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 Code: 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>
Plugin Initialization:插件初始化:
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)
}
Unfortunately, this code for some reason does not show the number of days when choosing a period.不幸的是,由于某种原因,此代码在选择一个时期时没有显示天数。 It is necessary, for example, to show "You have chosen - 5 days."
例如,有必要显示“您已选择 - 5 天”。
How to fix the code so that everything works?如何修复代码以便一切正常? Need your help!
需要你的帮助! Thanks!
谢谢!
The #days
is an input
element which doesn't have any HTML content appending doesn't make any change in the value. #days
是一个没有附加任何 HTML 内容的input
元素,不会对值进行任何更改。 To make it work use val()
method.要使其工作,请使用
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.