2015-11-19 38 views
1

我正在使用this jquery autocomplete插件。但是,当我搜索点击过滤结果我得到这个错误:JQuery自动完成:未捕获TypeError:无法读取未定义的属性'值'

遗漏的类型错误:无法读取的不确定

这里属性“价值”从检查控制台的堆栈跟踪:

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 
    } 
}); 

查看源代码,这个问题引起了当functi在onSelect()被调用是因为数组中没有建议。

为什么这个数组是空的?我选择一个过滤值,因此应该有一个元素

+0

'devbridgeAutocomplete' ?? – Jai

+0

@jai https://github.com/devbridge/jQuery-Autocomplete#known-issues – Ejaz

+0

我无法找到'valueField'和'keyField'的定义位置。 – kernel

回答

0

我已经找到了解决问题的办法:

这里是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); 
    } 
}, 
相关问题