【问题标题】:JQuery Auto Complete substitute for Select Drop DownjQuery Auto Complete 替代 Select Drop Down
【发布时间】:2011-12-09 19:12:21
【问题描述】:

我在文本输入上使用 jQuery 自动完成小部件来替换选择下拉列表。当用户单击文本框时,会打开建议下拉菜单。我的解决方案在 FireFox 中运行良好,但在 Internet Explorer 8 中出现了一些故障。在 Internet Explorer 中,当从建议下拉列表中选择项目时,建议列表消失,然后重新出现一秒钟。我不知道如何防止这种情况。

我正在使用: (jquery) jquery-1.6.4.min.js (jquery UI) jquery-ui-1.8.16.custom.min.js

代码如下

<input type="text" style="width:200px;" id="txtPosTypeS" value="" />

var RegTempList = [
{ label: "Auxiliary Monthly Trust", value: 1000},
{ label: "Auxiliary Monthly Operating", value: 1001},
{ label: "Auxiliary Hourly Trust", value: 1002},
{ label: "Auxiliary Hourly Operating", value: 1003}]

 $().ready(function() {
    $('#txtPosTypeS').autocomplete({
        minLength: 0,
        source: RegTempList,
        delay: 0,
        focus: function( event, ui ) {
            $(this).val( ui.item.label );
            return false;
        },
        select: function( event, ui ) {
            $(this).blur();
            $(this).val( ui.item.label );
            return false;
        },
        change: function (event, ui) {
            //if the value of the textbox does not match a suggestion, clear its value
            if ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0) {
                $(this).val('');
                $('#hidPositionType').val('');
            }
        },
        close: function(event, ui) {
            $(this).blur();
            return false;
        }
    })
    .focus(function(){
        $(this).autocomplete('search','');
    })
    .data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.label + "</a>" )
            .appendTo( ul );
    }; });

【问题讨论】:

  • 你不见了:$.expr[':'].textEquals = function(a, i, m) { return $(a).text().match("^" + m[3] + "$"); };
  • 您在数组末尾缺少分号。

标签: jquery jquery-ui jquery-autocomplete


【解决方案1】:

使用IE8和jsfiddle,change函数中textEquals出现脚本错误。删除更改功能,解决问题。

把这个扔进 jsFiddle,这里是链接。

http://jsfiddle.net/BDd9H/

另外,更新了文本框的宽度,使文本不会跳转。

【讨论】:

  • 您好,我刚刚在 IE 8 上尝试了您的示例,当您选择其中一项建议项目时,它仍然会闪烁建议下拉菜单。我正试图摆脱 IE 上烦人的重新弹出建议。
  • 它可以工作,但问题是它没有设置与下拉列表相同的值!
猜你喜欢
  • 2017-03-09
  • 1970-01-01
  • 2019-07-24
  • 2015-03-21
  • 2017-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多