如果输入日期2< =日期1,则JS警报

时间:2016-09-09 01:47:18

标签: javascript date alert

我有一个表格,访客可以在到达日期和出发日期进行住宿预订。如果他们单击输入字段,则会弹出一个日历以供他们选择日期。如果出发日期是< =到达日期,如何添加弹出式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>

1 个答案:

答案 0 :(得分:1)

比较两个日期一样简单,如下:

<img src="/images/home-desktop.jpg" srcset="/images/home-iphone6s.jpg 670w, /images/iphone6plus.jpg 1122w, /images/allMobile.jpg, 240w, /images/home-desktop.jpg 768w">

我建议你在用户输入到达日期时运行上面的if语句。因此,您必须在每个输入上使用onChange事件,以便它们返回并更改出发日期!