jQuery ui - datepicker阻止刷新onSelect

Pie*_*NAY 10 jquery-ui refresh datepicker qtip

我正在使用jQuery ui Datepicker来显示一个完整的"特殊日期"(带颜色)的年度内联日历. 在此输入图像描述 这是为了允许用户通过选择范围和其他一些细节来批量特殊日期.

$('#calendar').datepicker({
  ...
  , onSelect: function (selectedDate, inst) {
      $('.date_pick').toggleClass('focused');
      if ($('.date_pick.end').hasClass('focused')) {
        $('.date_pick.end').val('');
      }
      # inst.preventDefault() ? <- not a function
      # inst.stopPropagation() ? <- not a function
      # return (false) ? <- calendar refreshes anyway
    }
  ...
});
Run Code Online (Sandbox Code Playgroud)

我也使用qtip来显示每个日期的详细信息

我的问题是,当我点击日历时,它完全重新加载,所以我放松了我的qtips.

我不想在qtip中使用live()因为我不喜欢这种行为.

我也更喜欢每次点击它时日历都不刷新(但这似乎不可能)但我可能再也不能突出我的选择了.

你对我的问题有什么建议吗?

小智 25

我遇到了类似的问题.我在datepicker的底部添加了自定义按钮(使用$(id).append),但是当我选择日期时,datepicker会刷新并销毁它们.

这是jquery-ui库中datepicker的日期选择函数:

_selectDate: function(id, dateStr) {
  ...
    if (onSelect)
        onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]);
  ...
    if (inst.inline)
        this._updateDatepicker(inst);
  ...
},
Run Code Online (Sandbox Code Playgroud)

如您所见,该函数首先调用onSelect事件,然后调用_updateDatepicker(这是重绘表单的内容),如果inst.inline为true.

这是我的解决方法,以防止表单刷新,同时保持选择功能:

$("#cal_id").datepicker({
  onSelect: function(date, inst){

    //This is the important line.
    //Setting this to false prevents the redraw.
    inst.inline = false;

    //The remainder of the function simply preserves the 
    //highlighting functionality without completely redrawing.

    //This removes any existing selection styling.
    $(".ui-datepicker-calendar .ui-datepicker-current-day").removeClass("ui-datepicker-current-day").children().removeClass("ui-state-active");

    //This finds the selected link and styles it accordingly.
    //You can probably change the selectors, depending on your layout.
    $(".ui-datepicker-calendar TBODY A").each(function(){
      if ($(this).text() == inst.selectedDay) {
        $(this).addClass("ui-state-active");
        $(this).parent().addClass("ui-datepicker-current-day");
      }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)