我正在使用this jquery autocomplete插入。但是当我搜索单击过滤结果时,我收到此错误:
未捕获类型错误:无法读取未定义的属性“值”
这是来自检查器控制台的堆栈跟踪:
Uncaught TypeError: Cannot read property 'value' of undefined
Autocomplete.onSelect @ jquery.autocomplete.min.js:915
Autocomplete.select @ jquery.autocomplete.min.js:850
(anonymous function) @ jquery.autocomplete.min.js:195
n.event.dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3
这是我调用的方法:
var url = $(item).data('url');
var keyField = $(item).data('keyField') !== undefined ? $(item).data('keyField') : 'id';
var valueField = $(item).data('valueField') !== undefined ? $(item).data('valueField') : 'description';
$(myItem).devbridgeAutocomplete({
serviceUrl: myUrl,
minChars: 0,
type: 'post',
deferRequestBy: 500,
transformResult: function (response) {
var json_response = $.parseJSON(response);
var suggestions = $.map(json_response.items, function (dataItem) {
var interface = $('.content-wrapper').interface();
return {value: dataItem[valueField], data: dataItem[keyField]};
});
return {suggestions: suggestions};
},
onSelect: function (suggestion) {
// Do my stuff to populate the view
}
});
查看源代码,调用函数 onSelect()
时会出现问题,因为数组中没有建议。
为什么该数组为空?我正在选择一个过滤值,因此应该有一个元素
最佳答案
我找到了问题的解决方案:
这是issue我已经在GitHub上打开了。问题出在 onSelect()
函数上。
I refer to the version 1.2.24
onSelect: function (index) { var that = this, onSelectCallback = that.options.onSelect, suggestion = that.suggestions[index]; that.currentValue = that.getValue(suggestion.value); if (that.currentValue !== that.el.val() && !that.options.preserveInput) { that.el.val(that.currentValue); } that.signalHint(null); that.suggestions = []; that.selection = suggestion; if ($.isFunction(onSelectCallback)) { onSelectCallback.call(that.element, suggestion); } },
I've changed the method to:
onSelect: function (index) { var that = this, onSelectCallback = that.options.onSelect, suggestion = that.suggestions.length >= 1 ? that.suggestions[index] : that.suggestions; that.currentValue = that.getValue(suggestion.value); if (that.currentValue !== that.el.val() && !that.options.preserveInput) { that.el.val(that.currentValue); } that.signalHint(null); that.suggestions = []; that.selection = suggestion; if ($.isFunction(onSelectCallback)) { onSelectCallback.call(that.element, suggestion); } },
关于javascript - JQuery 自动完成 : Uncaught TypeError: Cannot read property 'value' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33800886/