【问题标题】:Set value of jQuery datepicker to value of another datepicker将 jQuery datepicker 的值设置为另一个 datepicker 的值
【发布时间】:2017-09-01 12:50:20
【问题描述】:

我在一个页面上有 2 个datepickers。预计用户将输入 2 个彼此有些接近的日期。但是,如果用户提前 6 个月跳过,他们的直觉告诉他们,当他们点击第二个 datepicker 时,它已经在同一个月份。

我需要将第二个datepicker的defaultDate设置为第一个datepicker中选择的日期。

$("#from").datepicker({
  defaultDate: new Date(),
  minDate: new Date(),
  onSelect: function(dateStr) 
  {         
      $("#to").val(dateStr);
      $("#to").datepicker("option",{ minDate: new Date(dateStr)})
  }
});

$('#to').datepicker({
  defaultDate: new Date(),
  onSelect: function(dateStr) {
    toDate = new Date(dateStr);
    fromDate = ConvertDateToShortDateString(fromDate);
    toDate = ConvertDateToShortDateString(toDate);
  }
});
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input id="from">
<input id="to">

但它无法运行。事实上,它根本无法初始化datepickers。我不知道为什么。让它们像平常一样datepickers 当然可以。

还有其他方法可以实现我的目标吗?

【问题讨论】:

  • 另外...忽略此处的 sn-p。它没有 jQuery 1.12,所以不能真正用作测试方法。
  • 编辑:我看到你更新了你的答案。请参阅包含 jquery edge 和 jquery ui 的此小提琴,此代码工作正常。 jsfiddle.net/gLrumpo3/5

标签: jquery date datepicker


【解决方案1】:

使用jquery ui的“show”方法

    $('#to').datepicker({

        });

$("#from").datepicker({
            defaultDate: new Date(),
            minDate: new Date(),
            onSelect: function(dateStr) 
            {   
                    $("#from").datepicker( "hide" );
                $('#to').val(dateStr);
                $('#to').focus();
                $('#to').datepicker( "show" );
                $("#to").datepicker( "option", "minDate", endDate );
                        //$("#to").datepicker( "option", "maxDate", '+2y' );
            }
        });

它被要求展示它的想法

我留下一个 jsfiddle:https://jsfiddle.net/roberburnday/btn7bp2v/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多