使用Jquery的Datepicker在选择日期后失去焦点到文本框。

使用Jquery的Datepicker在选择日期后失去焦点到文本框。 我正在使用jquery-ui-1.9.2。当选择日期时,焦点不会出现在文本框中。任何解决方案?

尝试使用以下代码。

HTML代码:

JQuery的:

 $("#date").datepicker({ onClose: function () { $(this).focus(); } }); 

JSFiddle1

编辑:上面的代码在IE中有问题,datepicker没有关闭。 在这篇博客中,您可以找到更多信息。

  // download and add this $("#date").datepicker({ /* fix buggy IE focus functionality */ fixFocusIE: false, 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; } }); 

JSFiddle2

  $(".datepicker").datepicker({ onClose: function () { $(this).parents().nextAll().find($(":input[type !='hidden']")).first().focus(); } }); }); 

我找到了一种更简单的方法,将重点放在下一个输入上,无论它是如何嵌套的。 你可以随时将.find之后的条件换成你喜欢的任何条件,它会把焦点放在那里。

在Doc Ready上初始化所有datepcikers

  $('.datepicker').datepicker( { onClose: function () { this.focus(); } }); 

解读Praveen的答案。 我有一个问题。 在IE上,datepicker拒绝出现每个奇怪的时间,我专注于一个字段。

此外,该解决方案存在轻微的逻辑问题(这不会影响任何事情,但我的眼睛仍然不正确):fixFocusIE字段正在设置选项,但后来它被调用“this”,当“这个” “指的是DOM元素,而不是选项对象。 所以基本上有两个fixFocusIE – 一个在选项(未使用),第二个在DOM元素本身。

并且$ .browser.msie也不再工作了,我不得不发明自己的IE探测器。

我的工作代码看起来像这样:

 var el = $("selector of element I need to assign to datepicker"); var options = {}; // actually I fill options from defaults and do some other manipulations, but I left it as empty object here for brevity options.fixFocusIE = false; function detectIE() { var ua = window.navigator.userAgent; if(ua.indexOf('MSIE ') > 0 || ua.indexOf('Trident/') > 0 || ua.indexOf('Edge/') > 0) { return true; } // other browser return false; } /* blur() and change() are needed to update Angular bindings, if any */ options.onSelect = function(dateText, inst) { options.fixFocusIE = true; $(this).blur().change().focus(); }; options.onClose = function(dateText, inst) { options.fixFocusIE = true; this.focus(); }; options.beforeShow = function(input, inst) { var result = detectIE() ? !options.fixFocusIE : true; options.fixFocusIE = false; return result; }; /* and this one will reset fixFocusIE to prevent datepicker from refusing to show when focusing the field for second time in IE */ el.blur(function(){ options.fixFocusIE = false; }); el.datepicker(options);