如何在关闭第一个 UI Datepicker 时打开第二个 UI Datepicker

发布于 2024-11-17 03:45:09 字数 732 浏览 2 评论 0原文

我正在尝试使用 JQuery UI 的 Datepicker 向表单添加一些功能。

当用户选择到达日期 (onSelect) 时,应该发生一些事情:

  1. < Strike>将出发日期更新为到达日期后 3 天(现在有效:))
  2. 将出发日期分为 3 部分,并将每个部分添加到输入字段(请参阅 Fiddle 中的到达日期示例)
  3. 当到达日期选择器关闭(onClose),出发日期选择器应自动打开,默认为第 1 点中选择的日期(例如: http://www.kayak.es

我设法得到第 1 点到工作,但我对其他功能有点迷失。如果有人能告诉我如何做到这一点或让我走上正确的道路,那就太好了!我还是一个初学者,但学得很快。

这是我现在拥有的小提琴: http://jsfiddle.net/mattvic/B6Kax/13/

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.net/mattvic/B6Kax/13/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨与橙与城 2024-11-24 03:45:09

将出发日期分成 3 份并添加
每个部分到一个输入字段

看起来您已经在目标日期选择器的 onSelect 处理程序中为此编写了代码。不幸的是,看起来 setDate 不会触发该事件,并且您无法手动触发。我建议将该代码分解为一个单独的函数,您可以从两个地方调用该函数:

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]);
}

然后更改您的出发日期选择器 onSelect 处理程序以指向该函数:

$('#vertrek').datepicker({

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

最后,从您到达的 < code>onSelect 事件处理程序:

/* 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());

当到达日期选择器关闭时
(onClose),出发日期选择器
应该自动打开,默认
到第 1 点中选择的日期

这只是使用 onClose 事件处理程序的问题:

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

这是更新的示例:

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 recommend 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.net/zXMke/

放我走吧 2024-11-24 03:45:09

回答标题的问题:

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

来源:

http://api.jqueryui.com/datepicker/#method-显示

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

我建议使用 onSelect 而不是 onClose 事件,以确保在打开之前已选择第一个日期第二个日期选择器。

Answering the title's question :

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

Source :

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

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

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

笑忘罢 2024-11-24 03:45:09

第3点答案:

$(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 );
           }                
        });
}); 

你已经有了第2点的解决方案吗?

干杯,
爸爸

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文