gpt4 book ai didi

javascript - 如何为日期选择器创建日期范围天间隔?

转载 作者:行者123 更新时间:2023-12-03 04:58:27 25 4
gpt4 key购买 nike

我正在使用Pikaday Plugin我根据自己的需要创建了预订表格,在我看来,除了一个问题之外,目前它很好。

有一个日期范围函数,我为两个输入日期之间设置了 15 天 if you click this link you will see

我有相同的表单,具有自动下一个焦点,但我在 15 天内无法应用我的日期范围函数,如何应用下面的代码

and by the way this is my auto next form link

function getMaxDate(element){
if(element.id=='checkout')
return new Date(new Date(element.parentNode.querySelector('#checkin').value).getTime()+(15*24*60*60*1000));
else
return new Date(2020, 12, 31);
}
function getMinDate(element){
if(element.id=='checkout')
return new Date(element.parentNode.querySelector('#checkin').value);
else
return new Date();
}

最佳答案

您可以查看下面的代码,了解我们如何验证 15 天范围内的日期。您试图在加载时验证结帐日期,因此他无法获取 checkin 日期,这就是它不起作用的原因。

$(document).ready(function() {

$('.flexdatalist').flexdatalist({
minLength: 0,
searchContain: true,
}).on('select:flexdatalist', function() {
$('#checkin').trigger("click");
});

assignPicker = function(id, whenClosed) {
if (typeof whenClosed !== 'function') {
whenClosed = null;
}

return new Pikaday({
numberOfMonths: 2,
field: document.getElementById(id),
format: "DD.MM.YYYY",
minDate: new Date(),
firstDay: 1,
maxDate: getMaxDate(id),
onSelect: function() {
e = this.getDate();
},
onSelect: whenClosed
});
}

assignPicker('checkin', function() {

$('#checkout').trigger("click");
});

$("#checkout").on('click',function(){
assignPicker('checkout', function() {
$('#select').trigger("click");
});
})


});
function getMaxDate(id){
if(id=='checkout')
return new Date(new Date($('#checkin').val()).getTime()+(15*24*60*60*1000));
else
return new Date(2020, 12, 31);
}
body {
padding: 30px;
}
input,
select {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
}
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/css/pikaday.min.css" />
<link rel="stylesheet" href="https://cdn.anitur.com.tr/example/flexdatalist/flexdatalist.css" />

</head>

<body>
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3">
<input type="text" name="" class='flexdatalist' data-min-length='1' list='languages' name='language' />
<datalist id="languages">
<option value="PHP">PHP</option>
<option value="JavaScript">JavaScript</option>
<option value="Cobol">Cobol</option>
<option value="C#">C#</option>
<option value="C++">C++</option>
<option value="Java">Java</option>
<option value="Pascal">Pascal</option>
<option value="FORTRAN">FORTRAN</option>
<option value="Lisp">Lisp</option>
<option value="Swift">Swift</option>
<option value="ActionScript">ActionScript</option>
</datalist>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<input type="text" class="checkin" id="checkin" />
</div>
<div class="col-lg-3 col-md-3 col-sm-3">

<input type="text" class="checkout" id="checkout" />
</div>
<div class="col-lg-3 col-md-3 col-sm-3">

<form action="" method="get">
<select name="select" id="select">
<option value="0">Choose</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
</form>
</div>
</div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/pikaday.min.js"></script>

<script src="https://cdn.anitur.com.tr/example/flexdatalist/flexdatalist.js"></script>

</body>

</html>

关于javascript - 如何为日期选择器创建日期范围天间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42340212/

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