jQuery UI的日期选择器 - ONSELECT获得所选日期3天(jQuery UI Date

2019-06-24 07:49发布

I try to create a date range by using the jQuery UI datepicker, using two text fields. The first text field "start_date" will set the start date and the second text field "end_date" will set the end date.

The code I have till now is this:

$('#start_date').live(
    'focus',
    function()
    {
        $('#end_date').datepicker(
            {
                dateFormat: 'dd MM yy',
                minDate: new Date(),
                maxDate: new Date(2012, 9, 15),
                stepMonths: 2,
                numberOfMonths: 2
            }
        );

        $(this).datepicker(
            {
                dateFormat: 'dd MM yy',
                minDate: new Date(),
                maxDate: new Date(2012, 9, 15),
                stepMonths: 2,
                numberOfMonths: 2,
                onSelect: function(dateText, inst)
                {
                    var instance = $( this ).data("datepicker");

                    var date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);

                    $('#end_date').datepicker('option', 'minDate', dateText);
                }
            }
        );
    }
);

NOTE : I use the live and the focus event because all my page content is loaded with AJAX call. Maybe that is correct or wrong, but this is not what I like to ask here ;)

The above code is correct for me and work fine, but what I like to do, is to set the value of the 'end_date' element to selected one +3 days.

Until now, when I choose a date at the "start_date" element the "end_date" element change to the same date as the "start_date", and this is what I like to change. The "end_date" to be +3 days from the "start_date" element adter the selection.

How can I do that ?

Answer 1:

要+1服务日成立END_DATE

onSelect: function(dateText, inst) {
    $('#start_date').datepicker('option','minDate', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay));
    var toDate = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);//Date one month after selected date
    var oneDay = new Date(toDate.getTime()+86400000);
    document.getElementById('end_date').value =$.datepicker.formatDate('dd/mm/yy', oneDay);
}


Answer 2:

演示你好Http://Jsfiddle.Net/96Lbf/

该演示将设置to文本框+3 days服用`从日期选择考虑。

jQuery代码

   $(function() {
        $( "#from, #to" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 1,
            onSelect: function( selectedDate ) {
                if(this.id == 'from'){
                  var dateMin = $('#from').datepicker("getDate");
                  var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1); 
                  var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 3); 
                  //$('#to').datepicker("option","minDate",rMin);
                  //$('#to').datepicker("option","maxDate",rMax);  
$('#to').val($.datepicker.formatDate('mm-dd-yy', new Date(rMax)));                    
                }

            }
        });
    });

HTML

<div class="demo"> 

<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>​


Answer 3:

如果一个月= 4,我想看看CAR 1的画面。 如果一个月= 5,我希望看到的Car3图片。 我该怎么做?

//..............
onSelect: function( selectedDate ) {
  if(this.id == 'from'){
    var dateMin = $('#from').datepicker("getDate");
    var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 0);
    alert (rMin.getMonth());
    if(rMin.getMonth() == 4){ 
      var image1=new Image();
      image1.src="car1.jpg"
      if(rMin.getMonth() ==5){
        var image2=new Image()
        image2.src="car3.jpg";
      }                           
    }
  }
}

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

    <label for="pics">pics</label> 
    <input type="image"
           src="car1.jpg"
           id="pics"
           name="pics"
           alt="Submit"
           width="48" height="48">

...............


文章来源: jQuery UI Datepicker - onSelect get the selected date +3 days