自动完成组合框无法选择选项

时间:2014-04-23 10:12:44

标签: jquery jquery-ui combobox autocomplete

我想使用jqueryui autocomplete组合框功能让用户从列表中选择一个选项。

我按照example进行了操作,这是jsfiddle中的代码。

当我点击列表中的选项时,我收到此错误:

Uncaught TypeError: Cannot read property 'option' of undefined.

错误指向以下行:

select: function(event, ui) {
     ui.item.option.selected = true;               <-- This Line
     self._trigger("selected", event, {
     item: ui.item.option
  })
},

option有什么问题?

编辑2:

似乎ui.item未分配给任何值,因为当我想向控制台显示ui.item时,它会返回undefined而不是object

但是,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

如果您提到的代码是自动填充

,请尝试使用值代替选项

i.item.value这将为您提供组合中选定的字符串类型值

例如,我想从我的组合(#emailstext)

中获取所选电子邮件的价值
$('#emailstext').autocomplete({
    source: dat,
    minLenght: 1,
    autocomplete: true,
    select: function (e, i) {
        dowhateverwithemail(i.item.value); //this will pass selected value from combo to function
    }
});

答案 1 :(得分:0)

似乎问题是jqueryui版本。在1.10.x版中,数据不再存储在item.autocomplete中,而是ui-autocomplete-item

它应该是:

input.data("ui-autocomplete")._renderItem = function(ul, item) {
    return $("<li></li>").data("ui-autocomplete-item", item).append("<a>" + item.label + "</a>").appendTo(ul);
};

而不是

input.data("ui-autocomplete")._renderItem = function(ul, item) {
    return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul);
};

您可以参考here

相关问题