gpt4 book ai didi

javascript - 如何使用php在下拉列表中自动更新三天的日期

转载 作者:行者123 更新时间:2023-11-30 06:20:59 24 4
gpt4 key购买 nike

我有一个简单的三天下拉选项来选择一个日期,但我希望该日期将自动更新最近三天,并且它必须在下拉列表中。

这是我的代码...

<select class="col-md-7" name="dateinfo">
<option value="02-11-2018">02-11-2018</option>
<option value="03-11-2018">03-11-2018</option>
<option value="04-11-2018">04-11-2018</option>
</select>

这是我的另一个代码,我在其中使用了 datepicker ....

  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<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>

<script>
$( function() {
$( "#datepicker" ).datepicker({ minDate: -3, maxDate: "0" });
} );
</script>

<p>Date: <input type="text" id="datepicker"></p>

我想在下拉菜单中...

最佳答案

在这里,当您从输入框中选择一个日期时,我尝试了一种手动添加 future 3 天日期的方法。您可以通过在 date 对象中添加天数来获取新日期

例子:

var d1 = new Date(newDate);
d1.setDate(d1.getDate() + 2);

它获取后天的日期。

因此,您想在选择框中获取接下来 3 天的日期。因此,循环 3 次并将日期附加到 option 字段。

$("#getdate").on('change', function() {
$('#dates').find('option').remove().end();
var newDate=this.value;
for(var i=0;i<3;i++)
{
var d1 = new Date(newDate);
d1.setDate(d1.getDate() + i);
$("#dates").append('<option>'+d1.getDate()+'-'+(d1.getMonth()+1)+'-'+d1.getFullYear()+'</option>');
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-git.js"></script>
<input type="date" id="getdate"></input>
<select id="dates">
</select>
</body>
</html>

编辑

//$("#getdate").on('change', function() {
//$('#dates').find('option').remove().end();
//var newDate=this.value;
var newDate = new Date();
console.log(newDate.getDate());
for(var i=0;i<3;i++)
{
var d1 = new Date(newDate);
d1.setDate(d1.getDate() - i);
$("#dates").append('<option>'+d1.getDate()+'-'+(d1.getMonth()+1)+'-'+d1.getFullYear()+'</option>');
}
//});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-git.js"></script>

<select id="dates">
</select>
</body>
</html>

关于javascript - 如何使用php在下拉列表中自动更新三天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131113/

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