我无法想到实现结束日期总是大于开始日期的逻辑。这是我的视图代码,有两个字段Event End和Event Start。尝试实施如Validate that end date is greater than start date with jQuery中所示,但失败了。如何实现这个?
<div class="clearfix"></div>
<div class="control-group form-group">
<label class="control-label col-md-4" style="text-align:left">Event Start</label>
<div class="controls" style="margin-left:0px">
<div class="col-md-3 date" data-date="12-02-2012" data-date-format="dd-mm-yyyy" data-date-viewmode="years">
<input class="m-wrap col-md-11 m-ctrl-medium datepicker form-control" required="required" name="starttimedate" id="ed_starttimedate" type="text" value="<?php echo $_POST['starttime']?date('d-m-Y',$_POST['starttime']):date('d-m-Y'); ?> " />
<!-- <span class="add-on"><i class="fa fa-calendar" style="margin-top:5px"></i></span> -->
</div>
<div class="col-md-1"> </div>
<div class="col-md-3 bootstrap-timepicker-component">
<input class="m-wrap col-md-11 m-ctrl-medium timepicker-default form-control" required="required" value="<?php echo date('h:i A',$_POST['starttime']);?>" name="starttimetime" id="ed_starttimetime" type="text" />
<!-- <span class="add-on"><i class="fa fa-clock-o" style="margin-top:5px"></i></span>-->
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="clearfix"></div>
<div class="control-group form-group">
<label class="control-label col-md-4" style="text-align:left">Event End</label>
<div class="controls ">
<div class="col-md-3 date" data-date="12-02-2012" data-date-format="dd-mm-yyyy" data-date-viewmode="years">
<input class="m-wrap col-md-11 m-ctrl-medium datepicker form-control" required="required" name="endtimedate" id="ed_endtimedate" type="text" value="<?php echo $_POST['endtime']?date('d-m-Y',$_POST['endtime']):date('d-m-Y'); ?> " />
<!--<span class="add-on"><i class="fa fa-calendar" style="margin-top:5px"></i></span>-->
</div>
<div class="col-md-1"> </div>
<div class="col-md-3 bootstrap-timepicker-component">
<input class="m-wrap col-md-11 m-ctrl-medium timepicker-default form-control" required="required" value="<?php echo date('h:i A',$_POST['endtime']);?>" name="endtimedatetime" id="ed_endtimedatetime" type="text" />
<!-- <span class="add-on"><i class="fa fa-clock-o" style="margin-top:5px"></i></span>-->
</div>
</div>
SCRIPT IS:
<script>
$("#ed_endtimedate").change(function() {
var startDate = document.getElementById("ed_starttimedate").value;
var endDate = document.getElementById("ed_endtimedate").value;
if ((Date.parse(ed_endtimedate) <= Date.parse(ed_starttimedate))) {
alert("End date should be greater than Start date");
document.getElementById("ed_endtimedate").value = "";
}
});
</script>
答案 0 :(得分:4)
在你的if-case中,你实际上是在尝试引用输入字段的id而不是你已经定义的两个变量。将发生错误,因为脚本将尝试读取未定义的两个变量。请参阅下面的修复
<script>
$("#ed_endtimedate").change(function() {
var startDate = document.getElementById("ed_starttimedate").value;
var endDate = document.getElementById("ed_endtimedate").value;
if ((Date.parse(endDate) <= Date.parse(startDate))) {
alert("End date should be greater than Start date");
document.getElementById("ed_endtimedate").value = "";
}
});
</script>
&#13;
重新阅读您的代码后,我发现您使用的日期格式无效。日期格式的ISO标准是yyyy-mm-dd,但你使用的是dd-mm-yyyy。