javascript - JQuery 自动完成 : Uncaught TypeError: Cannot read property 'value' of undefined

标签 javascript jquery autocomplete

我正在使用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/

相关文章:

javascript - 在 Meteor 中上传 POST 提交的图片

javascript - 使用javascript强制图像缓存

javascript - 如何删除自动完成中的一个特定值?

javascript - 根据列值或属性 href 列值将颜色设置为 FlexiGrid 行

jquery - 当一个函数结束时执行另一个函数

Android:使 AutoCompleteTextView 下拉菜单换行到 2 行或更多行

iphone - 如何控制/覆盖 iPhone 应用程序中的自动完成功能?

javascript - 将表单操作调用的 JSON 输出保存在 JavaScript 变量中?

javascript - 将代码注入(inject) iFrame 以隐藏特定元素

javascript - 重新排序单选按钮