2015-11-19 58 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); 
    } 
}, 
相關問題