gpt4 book ai didi

javascript - 如何通过选择标签更改日期选择器

转载 作者:行者123 更新时间:2023-12-01 02:45:27 25 4
gpt4 key购买 nike

我想使用选择标签更改日期选择器。例如我14号只在酒店住一晚,15号自动退房。更改自动退房日期已经可以..但是如果我使用选择标签更改自动退房日期酒店是否可以?例如,如果我选择两晚,那么结帐日期将变为 16 日

例如我在 jsfiddle 中提供的代码。

提前致谢

例如:https://jsfiddle.net/devefrontend/vaqdsmjk/7/

    $(document).ready(function() {

$("#dateStart").datepicker({
dateFormat: "dd-M-yy",
minDate: 'dateToday',
onSelect: function(date) {
var date2 = $('#dateStart').datepicker('getDate');
date2.setDate(date2.getDate() + 1);
$('#dateEnd').datepicker('setDate', date2);
//sets minDate to dt1 date + 1
$('#dateEnd').datepicker('option', 'minDate', date2);
}
}).datepicker("setDate", new Date());
$('#dateEnd').datepicker({
dateFormat: "dd-M-yy",
minDate: 1,
onClose: function() {
var dt1 = $('#dateStart').datepicker('getDate');
console.log(dt1);
var dt2 = $('#dateEnd').datepicker('getDate');
if (dt2 <= dt1) {
var minDate = $('#dateEnd').datepicker('option', 'minDate');
$('#dateEnd').datepicker('setDate', minDate);
}
}
}).datepicker("setDate", new Date());

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<label for="">start</label>
<input type="text" class="form-control" id="dateStart" name="start">
<label for="">end</label>
<input type="text" class="form-control" id="dateEnd" name="end">
<label for="">night</label>
<select name="night" id="night" class="form-control passenger">
<option selected="selected">1 night</option>
<option>2 night</option>
<option>3 night</option>
<option>4 night</option>
<option>5 night</option>
<option>6 night</option>
<option>7 night</option>
<option>8 night</option>
</select>

最佳答案

#dateStartonSelect中提取所有内容并将其转换为函数。在更改选择时调用此函数,而不是硬编码添加 1 晚,而是使用从下拉列表中选择的值:

function populateEndDate() {
var date2 = $('#dateStart').datepicker('getDate');
date2.setDate(date2.getDate() + parseInt($("#night").val()));
$('#dateEnd').datepicker('setDate', date2);
}

$(document).ready(function() {

$("#dateStart").datepicker({
dateFormat: "dd-M-yy",
minDate: 'dateToday',
onSelect: function(date) {
populateEndDate();
}
}).datepicker("setDate", new Date());
$('#dateEnd').datepicker({
dateFormat: "dd-M-yy",
minDate: 1,
onClose: function() {
var dt1 = $('#dateStart').datepicker('getDate');
console.log(dt1);
var dt2 = $('#dateEnd').datepicker('getDate');
if (dt2 <= dt1) {
var minDate = $('#dateEnd').datepicker('option', 'minDate');
$('#dateEnd').datepicker('setDate', minDate);
}
}
}).datepicker("setDate", new Date());

});

$("#night").on('change', function() {
populateEndDate();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<label for="">start</label>
<input type="text" class="form-control" id="dateStart" name="start">
<label for="">end</label>
<input type="text" class="form-control" id="dateEnd" name="end">
<label for="">night</label>
<select name="night" id="night" class="form-control passenger">
<option selected="selected" value=1>1 night</option>
<option value=2>2 night</option>
<option value=3>3 night</option>
<option value=4>4 night</option>
<option value=5>5 night</option>
<option value=6>6 night</option>
<option value=7>7 night</option>
<option value=8>8 night</option>
</select>

关于javascript - 如何通过选择标签更改日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47280098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com