jQuery的日期选择器调用onSelect不工作

jQuery的日期选择器调用onSelect不工作

本文介绍了jQuery的日期选择器调用onSelect不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是调用onSelect 事件,以设定的最低值结束日期等于值集作为开始日期。我有同样的code几页,但在其中的一个这是行不通的。如果我选择在开始日期,日期选择器不会显示一个值,当我走在结束日期字段。该页面加载并使用Ajax处理。

I am using the onSelect event in order to set the minimum value of the EndDate equal to the value set as a StartDate. I have the same code in several pages but in one of them it does not work. If I select a value in the StartDate, datepicker does not show when I go in the EndDate field. The pages are loaded and handled with ajax.

我的code是如下:

$(document).ready(function() {

$("#strStartDate").datepicker({
          dateFormat: 'dd/mm/yy',
          constrainInput: true,
          firstDay: 1,
          hideIfNoPrevNext: true,
          onSelect: function(){
                $("#strEndDate").datepicker( "option", "minDate", $("#strStartDate").val());
          }
     });

     $("#strEndDate").datepicker({
          dateFormat: 'dd/mm/yy',
          constrainInput: true,
          firstDay: 1,
          hideIfNoPrevNext: true
     });
 });

任何帮助是非常AP preciated。谢谢你在前进。

Any help is much appreciated. Thank you in advance.

使用背后的想法您的意见我能做到几乎我想要的东西。

Using the ideas behind your comments I manage to do almost the thing I want

$("#strStartDate").datepicker({
      dateFormat: 'dd/mm/yy',
      constrainInput: true,
      firstDay: 1,
      hideIfNoPrevNext: true,
 });

 $("#strEndDate").datepicker({
      dateFormat: 'dd/mm/yy',
      constrainInput: true,
      firstDay: 1,
      hideIfNoPrevNext: true,
      beforeShow: function (input, inst) {
            inst.settings.minDate = $("#strStartDate").val();
     }
 });

以上code从结束日期比然而,如果我们已经有结束日期值的起始日期更小的限制用户,并更改起始日期的值大于结束日期应该设置结束日期等于起始日期。这就是我试图做下面在我早期code中的code

The above code restricts user from putting an end date smaller than the start date however if there was already a value in end date, and you change the start date to a value bigger than the end date it should set the end date equal to the start date. This is what I was trying to do with the code below in my early code

onSelect: function(){
        $("#strEndDate").datepicker( "option", "minDate", $("#strStartDate").val());
     }

不知道如何使用实例来做到这一点,而不是重新初始化?

Any idea how to do this using the instance and not initialize again?

推荐答案

玉家伙......好消息......至少对我来说:)

Ok guys...Good news...at least for me:)

$("#strStartDate").datepicker({
      dateFormat: 'dd/mm/yy',
      constrainInput: true,
      firstDay: 1,
      hideIfNoPrevNext: true,
      onSelect: function(){
            if ($("#strStartDate").val() > $("#strEndDate").val()){
                $("#strEndDate").val($("#strStartDate").val());
            }
      }
 });

$("#strEndDate").datepicker({
      dateFormat: 'dd/mm/yy',
      constrainInput: true,
      firstDay: 1,
      hideIfNoPrevNext: true,
      beforeShow: function (input, inst) {
            inst.settings.minDate = $("#strStartDate").val();
     }
 });

这是最后的code。它只创建每个日期选择器的一个实例,当你想使用它作为一个日期范围选择器进行必要的验证。

This is the final code. It only creates one instance of each datepicker, and perform the necessary validation when you want to use it as a date range picker.

再次感谢您的指南

这篇关于jQuery的日期选择器调用onSelect不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:47