Jquery UI自动完成 – 由于特殊字符而导致的不需要的触发

我的应用程序中有一些自动完成function。 其中一些在加载时从数据库中填充。 如果输入值包含æøå等特殊字符,则自动完成会触发搜索,即使用户没有接近html输入。 这仅适用于Internet Explorer 11(可能更低)。 在FF和Chrome中,它可以像您期望的那样工作。

考虑以下输入:

 

如果将自动完成应用于此输入,其中一个可能的搜索结果与默认值(’charsæøå’)相同,则搜索将在初始化时触发。

JSfiddle在这里(使用IE看它在加载时触发): http : //jsfiddle.net/BY9gU/

我很乐意忽略IE,但不幸的是我的一些客户仍然使用它…

任何解决方法的想法?

我不久前遇到过这个问题,最后我在html中做了以下事情:

  

在我的自动完成初始化中,我有:

  $("input").autocomplete({ source: availableTags, create: function(event, ui) { $(this).val(($(this).attr("data-value"))); } }); 

看起来只有在value-attribute包含sspecial字符时才会发生这种情况。

尝试在文档的头部添加此代码:

  

我有同样的问题,但尚未找到修复(尚未)。

根据jQueryUI漏洞论坛,这是一个与IE相关而不是jQuery的问题。 他们没有答案,也不确定他们是否能够对这个问题采取措施。

http://bugs.jqueryui.com/ticket/9796#no1

正如@cverb在他的回答中所述,这是一个IE问题,不能归咎于jQuery UI。 但是,我已经做了一个似乎工作正常的解决方法,至少在我的项目中:

在jquery ui source中找到此代码:

 search: function( value, event ) { value = value != null ? value : this._value(); // always save the actual value, not the one passed as an argument this.term = this._value(); if ( value.length < this.options.minLength ) { return this.close( event ); } 

将其更改为此(添加if块):

 search: function( value, event ) { value = value != null ? value : this._value(); // The following if-block is inserted by me as a workaround. // Add all characters which may cause you trouble in the // regex pattern. if ( this._value().match(/[æøåÆØÅ]/) && this.term === undefined ) { return; } // always save the actual value, not the one passed as an argument this.term = this._value(); if ( value.length < this.options.minLength ) { return this.close( event ); } 

瞧! 有用。 我无法保证我没有打破其他一些function,但我99%肯定我没有:)