【问题标题】:Avoid reopen datepicker after select a date选择日期后避免重新打开日期选择器
【发布时间】:2012-10-22 11:25:48
【问题描述】:

仅在带有此代码的 IE 中

$('#datepicker').datepicker({
    onSelect : function(x, u){
     $(this).focus();   
    }
});

当我选择一个日期时,日期选择器会重新打开,因为我在onSelect 中添加了$(this).focus();。我该如何解决这个问题? (Example)

我正在使用 jquery 1.8.2 和 jquery-ui 1.9

【问题讨论】:

  • 这个链接可能对forum.jquery.com/topic/…有帮助
  • 选择后设置焦点有那么重要吗?在 datepicker API 中有许多事件可以利用。我的猜测是 jQuery UI 团队出于同样的原因移除了 IE 中的焦点
  • @al0neevenings ..我不明白该链接如何解决我的问题:(

标签: javascript jquery jquery-ui internet-explorer datepicker


【解决方案1】:

我今天遇到了这个问题,并为我提供了不同的解决方案。我的场景是我的 DatePicker 在一个 jQuery UI 对话框弹出窗口中。在 Chrome 中一切正常,但在 IE 中,日历总是会在选择日期后重新出现。

事实证明,在 jQuery 1.10.1 中有一个未解决的错误:http://bugs.jqueryui.com/ticket/9125

还有一个链接到的JSFiddle 演示了 IE 中的错误。有两种方法可以让它在 IE 中工作:

  1. modal 设置为false
  2. 选择日期后立即关注另一个元素

我选择了#2,这里是一个修复示例(只是更新了 JSFiddle 代码):

标记:

<div id="dialog">
    <input id="datepicker" />
    <input type='button' value='save' id='btnSave'/>    
</div>

JS:

$( "#datepicker" ).datepicker({onSelect: function() { $('#btnSave').focus(); }});
$( "#dialog" ).dialog({ modal: true });

希望这对将来的某人有所帮助!

【讨论】:

  • 非常详尽的回答,谢谢。我继续在 bugs.jqueryui.com 上投票支持该错误。此外,我最终也使用了您建议的#2 解决方案。我默认将焦点放在取消按钮上,以防他们不小心按回车键。
  • 嗯,实际上我在使用解决方案 #2 后遇到的一个问题是,我已将一个函数连接到创建 DatePicker 的文本框的更改事件......而更改事件没有t火了。因此,我没有使用解决方案 #2 ... 我将 类型的元素放在每个文本框/日期选择器之间,以便在我选择一个日期。在我的第二个日期选择器中选择一个日期后,它现在无害地将焦点设置在我的空跨度上。只需确保将 tabindex 属性设置为对您的页面有意义的值。
【解决方案2】:

这是我能得到的最好的解决方法:

(灵感来自this link

jsFiddle

$('#datepicker').datepicker({
            /* fix buggy IE focus functionality */
            fixFocusIE: false,    
            onSelect: function(dateText, inst) {
                  this.fixFocusIE = true;
                        $(this).change().focus();
            },
            onClose: function(dateText, inst) {
                  this.fixFocusIE = true;
                  this.focus();
            },
            beforeShow: function(input, inst) {
                  var result = $.browser.msie ? !this.fixFocusIE : true;
                  this.fixFocusIE = false;
                  return result;
            }
}).click(function(){$(this).focus()});

请注意您在问题中的建议,我使用的是 jquery-UI 1.9.0。在您的示例中,您使用的是 jquery-ui 1.8.15,已知它在某些事件上存在错误(例如 IE 上的 beforeShow())。

【讨论】:

  • +1 需要使用 1.9.0 jquery-ui 版本,因为之前版本中 beforeShow 事件的实现存在错误
  • 这在 jQuery 中不起作用 > IE 中的 1.9 jsfiddle.net/hv77F/22 如果有人有足够的时间为有价值的事业捐款,互联网需要一个解决方案!
  • @user1477388 -- 尝试添加一个空的(或者,对于 JSLint,一个 return undefined;onSelect 处理程序。还没弄清楚为什么会这样,但到目前为止它对我有用(YMMV)。
  • 对我不起作用(IE11)。到目前为止没有找到任何可行的解决方案。 BTW $.browser 在新的 jQuery 中已被弃用。
【解决方案3】:

我在 IE 上使用它,它也会阻止日期选择器重新打开

jQuery:

        onSelect: function(){ 
        $("#focusfix").focus();
    },

HTML(对话框 div 的第一行):

<input class="ui-helper-hidden-accessible" id="focusfix" type="text"autofocus/>

【讨论】:

    【解决方案4】:

    由于 .browser 在 jQuery 1.9 之后被删除,我不得不稍微修改代码。这对我有用:

    $('#datepicker').datepicker({
        /* fix buggy IE focus functionality */
        fixFocusIE: false,    
        onSelect: function(dateText, inst) {
              this.fixFocusIE = true;
                    $(this).change().focus();
        },
        onClose: function(dateText, inst) {
              this.fixFocusIE = true;
              this.focus();
        },
        beforeShow: function(input, inst) {
              var result = true;
              if (navigator.appName == 'Microsoft Internet Explorer') {
                result = !this.fixFocusIE;
              }
              this.fixFocusIE = false;
              return result;
        }
    }).click(function(){$(this).focus()});
    

    【讨论】:

    • IE 11 显然会为navigator.appName 以及 Chrome/Safari/Firefox 返回一个“Netscape”值。我没有检查这两个值,而是一起删除了条件,并且没有注意到非 IE 浏览器中的任何差异。谁能想到保留条件 (if (navigator.appName == 'Microsoft Internet Explorer') {) 的理由?
    【解决方案5】:

    试试这个:

    $('#datepicker').datepicker({
    onSelect : function(x, u){
     $(this).focus(); 
       $(this).datepicker("hide");     
    },
    onClose: function(e){
    e.preventDefault();
    }
    });
    

    这是小提琴http://jsfiddle.net/VZts7/69/

    或者对于IE8

    $('#datepicker').datepicker({
    onSelect : function(x, u){
     $(this).focus();     
    },
    onClose: function(e){
    (this).datepicker("hide");   
    }
    });
    

    即使它不是一个完美的解决方案,请参阅工作小提琴http://jsfiddle.net/hv77F/2/

    【讨论】:

    • 它可以工作,但是有一个异常“对象不支持属性或方法'preventDefault'”
    • 没什么!现在错误是“未定义事件”,因为我想“OnClose”只接受两个参数“dateTxt”和“inst”(datepicker 的实例)
    • with "e" 我看不到错误了,但是 datepicker 会重新打开 :(
    • 小提琴在 IE8 中的完整作品,但烤的答案比我的好,看他的答案
    【解决方案6】:

    IE 中 jQuery > 1.9 的解决方案

    {fixFocusIE: false},    
            {onSelect: function(dateText, inst) {
                  this.fixFocusIE = true;
                  $(this).change().focus();
    
                }
            },
            {onClose: function(dateText, inst) {
                  this.fixFocusIE = true;
                  this.focus();
    
                }
            },
            {beforeShow: function(input, inst) {
                  var result = isIE ? !this.fixFocusIE : true;                      
                  this.fixFocusIE = false;                      
                  return result;
             }
    
    function isIE() {
            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");
    
            if (msie > 0)      
               return true;
            else                 
    
            return false;
        }
    

    【讨论】:

      【解决方案7】:

      亡灵术。
      我遇到了同样的问题,除了这里提到的答案都不起作用。
      这可能是因为我必须处理的代码适用于 iframe,但我现在不确定。
      最重要的是,我无法更新到最新版本,因为它会破坏与我必须从事的其他项目的兼容性。
      所以这就是为我解决的问题:

      initDatePicker: function ()
      {
          // Initialize datepicker 
          var qs = document.getElementsByClassName("datepick");
          for (var i = 0; i < qs.length; ++i)
          {
              $(qs[i]).datepicker(
              {
                  // showOn: "button"
                  // showOn: "both"
                    showOn: "focus"
                  , buttonImage: "../images/cal.gif"
                  , buttonImageOnly: true
                  //, buttonImageOnly: false
                  , dateFormat: 'dd.mm.yy'
                  , changeMonth: true
                  , changeYear: true
                  , showWeek: false // true
                  , firstDay: 1
                  , showOtherMonths: true //false 
                  , selectOtherMonths: true
                  , showButtonPanel: true
      
      
                  //,onSelect : function(x, u){
                  //    // $(this).focus(); 
      
                  //    window.setTimeout(function () { $("#txtSchulungsTyp").focus(); $(this).datepicker("hide"); }, 300);
                  //},
                  //onClose: function(e){
                  //    // e.preventDefault();
                  //    e.preventDefault ? e.preventDefault() : e.returnValue = false;
                  //    return false;
                  //}
      
      
      
                  , onSelect: function ()
                  {
                      $(this).datepicker('disable');
                  }
                  , onClose: function ()
                  {
                      window.setTimeout(function (e)
                      {
                          $(e).datepicker('enable')
                      }.bind(undefined, this), 500)
                  }
      
              })
              ; // End datepicker
      
      
              // console.log(mGlobalLanguage);
      
              // $(qs[i]).datepicker("option", $.datepicker.regional['de']);  
              // $(qs[i]).datepicker("option", $.datepicker.regional['FR']);
          } // Next i 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-21
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 2019-03-15
        • 2019-09-28
        相关资源
        最近更新 更多