gpt4 book ai didi

javascript - 如果输入日期 2 <= 日期 1,则 JS 发出警报

转载 作者:行者123 更新时间:2023-12-03 06:09:10 25 4
gpt4 key购买 nike

我有一张表格,访客可以在其中预订住宿,并注明抵达日期和离开日期。如果他们单击输入字段,则会弹出日历供他们选择日期。如果出发日期<=到达日期,如何添加弹出式 JS 提醒?我的代码如下:

<head>
...
<script type="text/javascript" src="js/CalendarPopup.js"></script>
<script type="text/javascript">document.write(getCalendarStyles());</script>
...
</head
<script type="text/javascript" id="jsArrive">
var now = new Date();
var yesterday = new Date(now);
yesterday.setDate(now.getDate() - 1);
var calArrive = new CalendarPopup("divArrive");
</script>
<b>Arrive:</b>
<input type="text" name="dateArrive" value="" size="15" onClick="calArrive.select(document.forms[0].dateArrive,'dateArrive','d/M/yyyy'); return false;" title="calArrive.select(document.forms[0].dateArrive,'dateArrive','d/M/yyyy'); return false;" id="dateArrive" />
</p>
<div id="divArrive" style="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></div>

<script type="text/javascript" id="jsDepart">
var now = new Date();
var yesterday = new Date(now);
yesterday.setDate(now.getDate() - 1);
var calDepart = new CalendarPopup("divDepart");
</script>
<b>Depart:</b>
<input type="text" name="dateDepart" value="" size="15" onClick="calDepart.select(document.forms[0].dateDepart,'dateDepart','d/M/yyyy'); return false;" title="calDepart.select(document.forms[0].dateDepart,'dateDepart','d/M/yyyy'); return false;" id="dateDepart" />
<input type="submit" value="Go" name="submit" />
</p>
<div id="divDepart" style="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></div>

最佳答案

就像比较两个日期一样简单,如下所示:

if (departureDate <= arrivalDate) {
alert ("You must make sure your departure is before your arrival!);
}

我建议您在用户输入到达日期后运行上述 if 语句。因此,您可以在每个输入上使用 onChange 事件,以防它们返回并更改出发日期!

关于javascript - 如果输入日期 2 <= 日期 1,则 JS 发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39402618/

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