最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open second UI Datepicker on closing the first one - Stack Overflow

programmeradmin3浏览0评论

I´m trying to add some functionality to a form with JQuery UI´s Datepicker.

When a user selects the arrival date (onSelect), a few things should happen:

  1. Update the departure to 3 days after the arrival date (This works now :))
  2. Split the departure date in 3 and add each part to an input field (see arrival date example in Fiddle)
  3. When the arrival-datepicker closes (onClose), the departure-datepicker should open automatically, defaulting to the date selected in point 1 (example: )

I managed to get point 1 to work, but I´m a bit lost with the other features. If anyone can show me how to do this or get me on the right track that would be really great! I´m still a beginner but learning fast.

Here´s a Fiddle of what I have now: /

I´m trying to add some functionality to a form with JQuery UI´s Datepicker.

When a user selects the arrival date (onSelect), a few things should happen:

  1. Update the departure to 3 days after the arrival date (This works now :))
  2. Split the departure date in 3 and add each part to an input field (see arrival date example in Fiddle)
  3. When the arrival-datepicker closes (onClose), the departure-datepicker should open automatically, defaulting to the date selected in point 1 (example: http://www.kayak.es )

I managed to get point 1 to work, but I´m a bit lost with the other features. If anyone can show me how to do this or get me on the right track that would be really great! I´m still a beginner but learning fast.

Here´s a Fiddle of what I have now: http://jsfiddle/mattvic/B6Kax/13/

Share Improve this question edited Jun 26, 2011 at 18:21 Jonas 129k102 gold badges327 silver badges405 bronze badges asked Jun 26, 2011 at 17:52 MattvicMattvic 4268 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Split the departure date in 3 and add each part to an input field

Looks like you already have the code written for this in the onSelect handler of the destination datepicker. Unfortunately, it looks like setDate does not trigger that event, and you cannot trigger it manually. What I would remend is breaking that code out into a separate function that you can call from both places:

function splitDepartureDate(dateText) {
    var depSplit = dateText.split("-", 3);
    $('#alt-vertrek-d').val(depSplit[0]);
    $('#alt-vertrek-m').val(depSplit[1]);
    $('#alt-vertrek-y').val(depSplit[2]);
}

Then change your departure datepicker onSelect handler to point to that function:

$('#vertrek').datepicker({

    // Prevent selecting departure date before arrival:
    beforeShow: customRange,
    onSelect: splitDepartureDate
});

Lastly, call that function from your arrival's onSelect event handler:

/* snip: */

// Populate departure date field 
var nextDayDate = $('#aankomst').datepicker('getDate', '+3d');
nextDayDate.setDate(nextDayDate.getDate() + 3);
$('#vertrek').datepicker('setDate', nextDayDate);

// Manually call splitDepartureDate
splitDepartureDate($("#vertrek").val());

When the arrival-datepicker closes (onClose), the departure-datepicker should open automatically, defaulting to the date selected in point 1

This is just a matter of using the onClose event handler:

onClose: function() {
    $("#vertrek").datepicker("show");
}

Here's your updated example: http://jsfiddle/zXMke/

Answering the title's question :

 $("#firsDate").datepicker('option',"onClose", function() { $( "#secondDate" ).datepicker( "show" ); });

Source :

http://api.jqueryui./datepicker/#method-show

http://api.jqueryui./datepicker/#option-onClose

I remend to use onSelect instead of onClose event in order to make sure that the 1st date has been selected before opening the second datepicker.

point 3 answer:

$(function() {
  $( '#aankomst' ).datepicker({
    onClose: function(dateText, instance) {
      $( '#vertrek' ).datepicker('show');
     },      
    onSelect: function( dateText, instance ) {

      // Split arrival date in 3 input fields                        
      var arrSplit = dateText.split("-");
        $( '#alt-aankomst-d' ).val(arrSplit[0]);
        $( '#alt-aankomst-m' ).val(arrSplit[1]);
        $( '#alt-aankomst-y' ).val(arrSplit[2]);

       // Populate departure date field
       var nextDayDate = $( '#aankomst' ).datepicker('getDate', '+3d');
         nextDayDate.setDate( nextDayDate.getDate() + 3 );
           $( '#vertrek' ).datepicker( 'setDate', nextDayDate );
           }                
        });
}); 

you already have the solution for point 2?

Cheers, Daddy

发布评论

评论列表(0)

  1. 暂无评论