datepicker创建一个特定的日期范围

datepicker创建一个特定的日期范围

本文介绍了使用jQuery datepicker创建一个特定的日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试使用jQuery日期选择器来创建开始日期日历和结束日期压缩器。我正在使用这里的日期范围范例:



开始日期不能在今天的日期之前,结束日期可以是所选开始日期的30天。 / p>

例如,如果我在第一个datepicker中选择了5月17日的开始日期,那么第二个datepicker的结束日期只能在5月18日至6月18日期间选择。 / p>

这是我的代码:

 <!DOCTYPE html PUBLIC // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta charset =utf-8/>
< title> Untitled Document< / title>
< link href =http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css =stylesheettype =text / css />
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js>< / script>
< script src =http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js>< / script>

< script type =text / javascript>
$(function(){
var dates = $(#from,#to).datepicker({
defaultDate:+ 1w,
changeMonth:true,
numberOfMonths:2,
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>
< / head>

< body>
< div class =date>

< label for =from> From< / label>
< input type =textid =fromname =from/>
< label for =to> to< / label>
< input type =textid =toname =to/>

< / div><! - 结束演示 - >

< / html>

任何帮助将不胜感激。谢谢!

解决方案

你去:

  $ (function(){
$(#from,#to).datepicker({
defaultDate:+ 1w,
changeMonth:true,
numberOfMonths:3,
onSelect:function(selectedDate){
if(this.id =='from'){
var dateMin = $('#from')。datepicker(getDate);
var rMin = new Date(dateMin.getFullYear(),dateMin.getMonth(),dateMin.getDate()+ 1); // Min Date = Selected + 1d
var rMax = new Date(dateMin。 getFullYear(),dateMin.getMonth(),dateMin.getDate()+ 31); // Max Date = Selected + 31d
$('#to')。datepicker(option,minDate,rMin );
$('#to')。datepicker(option,maxDate,rMax);
}

}
});
});


I'm trying to use the jQuery date picker to create a start date calendar and an end date calender. I'm using the "date range" example seen here: http://jqueryui.com/demos/datepicker/#date-range

The start date can not be before today's date and the end date can be 30 days past the selected start date.

For example if I chose a start date of May 17th in the first datepicker, then the end date in the second datepicker can only be selectable for May 18th through June 18th.

Here's my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Untitled Document</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script type="text/javascript">
    $(function() {
        var dates = $( "#from, #to" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 2,
            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>
  </head>

<body>
<div class="date">

<label for="from">From</label>
<input type="text" id="from" name="from"/>
<label for="to">to</label>
<input type="text" id="to" name="to"/>

</div><!-- End demo -->

</html>

Any help would be appreciated. Thanks!

解决方案

There you go : http://jsfiddle.net/c0mm0n/SJhmF/3/

$(function() {
    $( "#from, #to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function( selectedDate ) {
            if(this.id == 'from'){
              var dateMin = $('#from').datepicker("getDate");
              var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1); // Min Date = Selected + 1d
              var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 31); // Max Date = Selected + 31d
              $('#to').datepicker("option","minDate",rMin);
              $('#to').datepicker("option","maxDate",rMax);
            }

        }
    });
});

这篇关于使用jQuery datepicker创建一个特定的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:44