jQuery UI 日期选择器到达-离开日期问题

发布于 2024-12-05 22:47:20 字数 1404 浏览 2 评论 0原文

啊...我很困惑...

我有一个运行 jQuery 日期选择器 UI 的预订模块。我有一个到达日期字段和一个出发日期字段,每个字段都有一个在两个字段上启动的日期选择器对象。

我希望默认到达日期是今天的日期,我通过执行以下操作实现了这一点:

   <script type="text/javascript">
      $(document).ready(function(){

          $(".arrive").datepicker();
          $(".depart").datepicker();

          $(".arrive").datepicker('setDate', new Date());

      });
   </script> 

这非常有效,但是当用户选择到达日期时,我希望将出发默认日期设置为用户选择到达日期后的一天。我尝试使用日期选择器对象的 onSelect 事件,并尝试从到达字段中提取值并添加一天,但无济于事。我以前见过这个功能,我认为这不是很困难,我只是对这个功能感到困惑。有什么想法吗?我的文本字段的 html 如下:

<ul>
  <li>  
    <label>Arriving</label>     
    <span class="dateinput">                                                   
        <input class="arrive" type="text" value="" name="startDate" />                                  
        <img id="arrive"  src="images/calendar_icon.png"  />
        <div class="clear"></div>
    </span>
  </li>

  <li>  
    <label>Departing</label>        
    <span class="dateinput">                                
        <input class="depart" name="endDate" value="" type="text"  />               
        <img src="images/calendar_icon.png" />
        <div class="clear"></div>
    </span> 
  </li>
</ul>

非常感谢任何可以帮助我解决此问题的人。

Argh.. I am stumped....

I have a reservation module that is running the jQuery date-picker UI. I have an arrival date field and a departure date field each with a date-picker object initiated on both.

I want the default arrival date to be todays date I achieved this by doing the following:

   <script type="text/javascript">
      $(document).ready(function(){

          $(".arrive").datepicker();
          $(".depart").datepicker();

          $(".arrive").datepicker('setDate', new Date());

      });
   </script> 

That works perfectly however when a user selects an arrival date I then want the departure default date to be set as one day after the users selected arrival date. I tried using the onSelect events for the date-picker object and tried to pull the value from the arrival field and add a day but to no avail. I have seen this functionality before, I don't think it is very difficult I am just beyond stumped on this one. Any ideas? My html for the text fields is as follows:

<ul>
  <li>  
    <label>Arriving</label>     
    <span class="dateinput">                                                   
        <input class="arrive" type="text" value="" name="startDate" />                                  
        <img id="arrive"  src="images/calendar_icon.png"  />
        <div class="clear"></div>
    </span>
  </li>

  <li>  
    <label>Departing</label>        
    <span class="dateinput">                                
        <input class="depart" name="endDate" value="" type="text"  />               
        <img src="images/calendar_icon.png" />
        <div class="clear"></div>
    </span> 
  </li>
</ul>

Thanks a bunch to whoever can help me with this issue.

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

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

发布评论

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

评论(3

起风了 2024-12-12 22:47:20

使用文档作为灵感:

<script type="text/javascript">
  $(document).ready(function(){

      $(".arrive").datepicker({
          onSelect: function( selectedDate ) {
            // Parse the selected date
            var instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );

            // Add one day
            date.setDate(date.getDate()+1);

            // Set the new date
            $(".depart").datepicker('setDate', date);               
        }
      });
      $(".depart").datepicker();

      $(".arrive").datepicker('setDate', new Date());
  });
</script> 

Using the documentation as inspiration:

<script type="text/javascript">
  $(document).ready(function(){

      $(".arrive").datepicker({
          onSelect: function( selectedDate ) {
            // Parse the selected date
            var instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );

            // Add one day
            date.setDate(date.getDate()+1);

            // Set the new date
            $(".depart").datepicker('setDate', date);               
        }
      });
      $(".depart").datepicker();

      $(".arrive").datepicker('setDate', new Date());
  });
</script> 
绝不放开 2024-12-12 22:47:20

你看过文档吗?

http://jqueryui.com/demos/datepicker/#date-range

-> ;查看源代码

它甚至有一些 onSelect-code 你可以部分使用你的特定问题

did you have a look at the documentation?

http://jqueryui.com/demos/datepicker/#date-range

-> view source

it even got some onSelect-code you could partly use for your specific problem

冷弦 2024-12-12 22:47:20

经过一番尝试和错误后发现这对我来说设置最短出发日期和最长抵达日期很有效(如果在抵达之前选择出发)

$("#arrival").datepicker({
  //set other options here
  minDate: 0,  //sets min date today
  onClose: function() {
    let arriveDate = $(this).datepicker("getDate");
    if(arriveDate){
      arriveDate.setDate(arriveDate.getDate() + 1);
      $("#depart").datepicker("option", "minDate", arriveDate); //sets min departure date to arrival date + 1
    }else $("#depart").datepicker("option", "minDate", 1); //reset to default min departure date (in case arrival date was earlier set and later removed)
  }
})
$("#depart").datepicker({
  //set other options here
  minDate: 1,  //sets min date today + 1
  onClose: function() {
    let departDate = $(this).datepicker("getDate");
    if(departDate){
      departDate.setDate(departDate.getDate() - 1);
      $("#arrival").datepicker("option", "maxDate", departDate); //sets max arrival date to departure date - 1
    }else $("#arrival").datepicker("option", "maxDate", ''); //reset to default max arrival date (in case departure date was earlier set and later removed)

  }
})

after some trial and error found this worked well for me to set a min departure date and max arrival date (in case departure selected before arrival)

$("#arrival").datepicker({
  //set other options here
  minDate: 0,  //sets min date today
  onClose: function() {
    let arriveDate = $(this).datepicker("getDate");
    if(arriveDate){
      arriveDate.setDate(arriveDate.getDate() + 1);
      $("#depart").datepicker("option", "minDate", arriveDate); //sets min departure date to arrival date + 1
    }else $("#depart").datepicker("option", "minDate", 1); //reset to default min departure date (in case arrival date was earlier set and later removed)
  }
})
$("#depart").datepicker({
  //set other options here
  minDate: 1,  //sets min date today + 1
  onClose: function() {
    let departDate = $(this).datepicker("getDate");
    if(departDate){
      departDate.setDate(departDate.getDate() - 1);
      $("#arrival").datepicker("option", "maxDate", departDate); //sets max arrival date to departure date - 1
    }else $("#arrival").datepicker("option", "maxDate", ''); //reset to default max arrival date (in case departure date was earlier set and later removed)

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