【问题标题】:jQuery ui - datepicker prevent refresh onSelectjQuery ui - datepicker 防止刷新 onSelect
【发布时间】:2011-05-19 09:10:53
【问题描述】:

我正在使用 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
    }
  ...
});

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

我的问题是当我点击日历时,它会完全重新加载,所以我失去了我的 qtips。

我不想将 live() 与 qtip 一起使用,因为我不喜欢这种行为。

我还希望日历在我每次单击时都不会刷新(但这似乎不可能),但我可能不再能够突出显示我的选择。

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

【问题讨论】:

  • 你试过Yozomiri的解决方案了吗?
  • 我可以在小提琴上看到你的代码吗?我正在尝试实现几乎相同的功能。如果可能,请。
  • 很抱歉这是一个旧项目,我不再访问代码。基本上我使用beforeShowDay 回调来应用CSS
  • 是onSelect和beforeShowDay的组合?您是否以这种方式处理 onSelect 每个日期应用自定义 css 或给它一个日期数组并使用 beforeShowDay 在它们上应用 css 类?我只需要一个想法。谢谢。
  • 不,选择 CSS 也以 beforeShowDay 为条件,因为单击会重新加载整个日期选择器

标签: jquery-ui datepicker refresh qtip


【解决方案1】:

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

这是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);
  ...
},

如您所见,该函数首先调用 onSelect 事件,然后如果 inst.inline 为 true,则调用 _updateDatepicker(即重绘表单)。

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

$("#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");
      }
    });
  }
});

【讨论】:

  • 1 天怎么样...它节省了很多天!谢谢 ! :) 有谁知道这是否会在未来的 jQuery-UI 中成为可设置的功能?
  • 这个答案是金子! 1000分!坐下! :-)
【解决方案2】:

在 onselect 内将 inst.inline 设置为 false 将不起作用。 而是尝试类似

onSelect: function() {
    $(this).data('datepicker').inline = true;                               
},
onClose: function() {
    $(this).data('datepicker').inline = false;
}

【讨论】:

    【解决方案3】:

    我也有几乎相同的问题,比如一些other people,我有某种解决方案....但这不公平:

    $('#calendar').datepicker({
    ...,
    onSelect: function (selectedDate, inst) 
    {
      myFunction(selectedDate, inst);
    }
    });
    function myFunction(selectedDate, inst)
    {
    
      $('.date_pick').toggleClass('focused');
      if ($('.date_pick.end').hasClass('focused')) {
        $('.date_pick.end').val('');
      }
      inst.preventDefault(); # aa; works too, but writing aa; is going too far xD
    }
    

    它并不完美,但可以工作......我会努力让它工作得很好,直到那时......

    编辑:解决添加:

    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    

    【讨论】:

    • inst.preventDefault()
    【解决方案4】:

    如果您只想选择一天,那么您必须在 JQuery 中指定月份和年份:

    $(".ui-datepicker-calendar TBODY [data-month='"+inst.selectedMonth+"'][data-year='"+inst.selectedYear+"'] A").each(function(){
    

    【讨论】:

      【解决方案5】:

      如果页面上有一些日期选择器 Yozomiri 示例将失败。你应该这样做:

              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.
                  $(this).find(".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.
                  $(this).find(".ui-datepicker-calendar TBODY td").each(function(){
                    if ( $(this).find('a').text() == inst.selectedDay && $(this).data('month') == inst.selectedMonth ) {
                      $(this).find('a').addClass("ui-state-active");
                      $(this).addClass("ui-datepicker-current-day");
                    }
                  });
              }
      

      https://jsfiddle.net/g2bgbdne/3/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-31
        • 2011-07-11
        • 2015-02-04
        相关资源
        最近更新 更多