English 中文(简体)
使用 jQuery Datecker 来比较开始日期和结束日期?
原标题:How can I compare a start date and end date using the jQuery Datepicker?

我为两个不同的文本字段使用 jQuery Datepaker 。一个字段用于起始日期,另一个字段用于结束字段。我用 minDate 关闭了当前日期之前的日期。我的代码是这样的:

$( "#start" ).datepicker({
    minDate: 0,
    dateFormat:  yyyy-mm-dd ,
    showOnFocus: true,
    showTrigger:  <img src="<?php echo base_url()?>images/calender.png" /> 
});    

$( "#end" ).datepicker({
    minDate: 0,
    dateFormat:  yyyy-mm-dd ,
    showOnFocus: true,
    showTrigger:  <img src="<?php echo base_url()?>images/calender.png" /> 
}); 

现在我需要检查结束日期必须等于或大于开始日期。 在结束日期字段中, 选中的开始日期之前的所有日期都必须被禁用 。 如何做到这一点?

最佳回答

首先,感谢所有帮助我找到答案的人。

解决我的问题的方法是使用以下代码:

$(document).ready(function(){
  $("#depart ,#return_date").datepick({
    minDate:0,
    dateFormat: yyyy-mm-dd ,
    showOnFocus: true,
    showTrigger:  <img src="<?php echo base_url()?>images/calender.png" /> ,
    onSelect: customRange
  });

  function customRange(dates) {
    if (this.id ==  depart )
    {
      $( #return_date ).datepick( option ,  minDate , dates[0] || null);
    }
    else
    {
      $( #depart ).datepick( option ,  maxDate , dates[0] || null);
    }
  }
});
问题回答

以下是需要的代码示例。

<script>
    $(function() {
        var dates = $( "#from, #to" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onSelect: function( selectedDate ) {
                var option = this.id == "from" ? "minDate" : "maxDate",
                    instance = $( this ).data( "datepicker" ),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings );
                dates.not( this ).datepicker( "option", option, date );
            }
        });
    });
    </script>

您可以在以下网址查看演示文稿:Datepicker

从Jquery UI网站查阅。

使用日期js 库用于复杂的日期相关操作 - & gt; datejs.com

you need to reset end date when start date changes. this should do it.

<script type="text/javascript">
    $(document).ready(function () {
        $( #start ).datepicker({
            minDate:  -1d ,
            dateFormat:  yy-mm-dd ,
            showTrigger:  <img src="<?php echo base_url()?>images/calender.png" /> 
        });

        $( #end ).datepicker({
            minDate:  -1d ,
            dateFormat:  yy-mm-dd ,
            showTrigger:  <img src="<?php echo base_url()?>images/calender.png" /> 
        });

        $( #start ).change(function () {
            var sDate = $( #start ).val().split("-");
            var endMinDate = new Date(sDate[0], sDate[1] - 1, sDate[2]);
            $( #end ).datepicker( option ,  minDate , endMinDate);
        });
    }); 
</script>




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签