">
我有两个输入字段的开始和结束日期,我需要如果用户选择开始日期,使只有14天可供选择的结束日期,我该怎么做?
下面是我的html代码:
<label for="start_date">Date From</label>
<input id="start_date" required="true" width="276"/>
<label for="end_date">Date To</label>
<input id="end_date" required="true" width="276"/>
下面是我的脚本:
<script>
$('#start_date').datepicker({
uiLibrary: 'bootstrap4'
});
</script>
<script>
$('#end_date').datepicker({
uiLibrary: 'bootstrap4',
numberOfMonths: 2
});
</script>
发布于 2019-12-12 09:24:13
为此,请使用max-date
和min-date
。
$("#start_date").datepicker({
onSelect: function(dateText, inst) {
var olddate = new Date($(this).val());
var newdate = new Date();
var numberOfDaysToAdd = 14;
newdate.setDate(olddate.getDate() + numberOfDaysToAdd);
$("#end_date").datepicker({
minDate: olddate,
maxDate: newdate
});
}
});
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script type="text/javascript" src="dist/jquery.daterangepicker.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<label for="start_date">Date From</label>
<input id="start_date" required="true" width="276"/>
<label for="end_date">Date To</label>
<input id="end_date" required="true" width="276"/>
https://stackoverflow.com/questions/59300426
复制相似问题