jQuery日期选择器动态minDate -issue

时间:2018-11-12 09:36:42

标签: javascript php jquery datepicker

我有一个日期选择器jquery函数

$("#txtFrom").datepicker({
  beforeShowDay: $.datepicker.noWeekends,
  numberOfMonths: 1,
  minDate: sprint_select(),
  maxDate: $( '#d_id' ).val(),
  onSelect: function (selected) {
    var dt = new Date(selected);
    $("#txtTo").datepicker("option", "minDate", dt);
  }
});

在这里我设置了minDate: sprint_select(),调用了函数sprint_select

function sprint_select(){
  var r_id=$('#r_id').val();
  var dataString = { r_id: r_id}

  $.ajax({
    url: "/manage/scrum/getsprintprd",
    method: "POST",
    data: dataString,
    //                   dataType: "json",
    //cache: false,
    success: function (data)
    {
      //var values= $.trim(data).split("-");
      var values1 = '2018-11-09'.split("-"); 
      var parsed_date = new Date(values[0], values[1]-1, values[2]);
      return parsed_date;
    }
  });
}

在这里我成功返回parsed_date。但是日历的最小日期没有变化。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试在sprint_select()成功时调用datepicker函数: (因为从api获取日期可能需要一些时间。在完成日期选择器功能之前。)

    function sprint_select(){
      var r_id=$('#r_id').val();
      var dataString = { r_id: r_id}

      $.ajax({
        url: "/manage/scrum/getsprintprd",
        method: "POST",
        data: dataString,
        //                   dataType: "json",
        //cache: false,
        success: function (data) {
          //var values= $.trim(data).split("-");
          var values1 = '2018-11-09'.split("-"); 
          var parsed_date = new Date(values[0], values[1]-1, values[2]);

           $("#txtFrom").datepicker({
           beforeShowDay: $.datepicker.noWeekends,
           numberOfMonths: 1,
           minDate: parsed_date ,
           maxDate: $( '#d_id' ).val(),
           onSelect: function (selected) {
           var dt = new Date(selected);
           $("#txtTo").datepicker("option", "minDate", dt);
         }
       });

    }
 });
}

答案 1 :(得分:0)

尝试一下,
maxDate喜欢:

<input type="hidden" id="d_id" name="d_id" value="01-28-2019">

脚本

var max = new Date($( '#d_id' ).val());
$("#txtFrom").datepicker({
    beforeShowDay: $.datepicker.noWeekends,
    numberOfMonths: 1,
    minDate: sprint_select(),
    maxDate: max,
    onSelect: function (selected) {
       var dt = new Date(selected);
       $("#to_date").datepicker({
          minDate: dt,
       });
    }
});

检查Ajax返回值,其格式应为mm / dd / yyyy

function sprint_select(){
    var parsed_date = new Date('01-02-2019');
    return parsed_date; 
}