weixin_39930276
weixin_39930276
2021-01-09 00:41

JQuery autocomplete: Uncaught TypeError: Cannot read property 'value' of undefined

Hi everyone, I'll send you a link to the Stackoverflow question I've made about the error I've titled this issue.

premise I'm using - JQuery v1.11.0 - devbridge/jQuery-Autocomplete v1.2.24

This is the error I'm facing


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

To resolve it I've made a change on the Autocomplete source code at the method onSelect: function (index)

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

This solved my problem. I don't know if any other user is facing the same problem.

该提问来源于开源项目:devbridge/jQuery-Autocomplete

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • weixin_39755136 weixin_39755136 4月前

    Your problem is that you are passing empty suggestion. Probably trailing comma in your suggestions array: [a,b,]. Having correct response that would not happen.

    点赞 评论 复制链接分享
  • weixin_39872334 weixin_39872334 4月前

    I have got 1 error on my page "Uncaught TypeError: Cannot read property 'label' of undefined". can you please help me to know reason for this error

    点赞 评论 复制链接分享
  • weixin_39755136 weixin_39755136 4月前

    There is no label in autocomplete code base. What you are doing, is trying to access property on an object that has no value:

    `javascript
    // Object has value of undefined
    var obj;
    
    // Uncaught TypeError: Cannot read property 'label' of undefined
    var label = obj.label;
    

    `

    点赞 评论 复制链接分享
  • weixin_39583162 weixin_39583162 4月前

    I have the same problem too. It would be great to check suggestion on undefined before trying to get the value in that.getValue(suggestion.value);

    点赞 评论 复制链接分享
  • weixin_39637203 weixin_39637203 4月前

    i can confirm this is still an issue in the most recent version. i have verified that the suggestion array is well-formed. and i can confirm that 's fix appears to have fixed the issue.

    点赞 评论 复制链接分享
  • weixin_39872044 weixin_39872044 4月前

    this issue is still there but 's solution did not work for me. Removing

    that.suggestions = [];
    from
    onSelect
    works for me. This does not set back the suggestions to empty and hence it is not
    undefined
    on the next click/select
    点赞 评论 复制链接分享

相关推荐