最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does jQuery autocomplete work with a dynamic array as source - Stack Overflow

programmeradmin3浏览0评论

I am currently trying to create an autocomplete with a source that is stored in a javascript variable but this variable can be updated by another function. So, what I would like is that at each time the user updates the autocomplete field, the source field of autocomplete is generated.

Here is the code I use:

<head>
    <script>
        var availableTags = ['java', 'javascript']
        // can be called anytime
        var addToTags = function(str){availableTags.push(str)}

        $(function() {
            $( "#tags" ).autocomplete({
                source: availableTags
            });
        });
    </script>
</head>
<body>
    <div class="ui-widget">
        <label for="tags">Tags: </label>
        <input id="tags" />
    </div>
</body>

Do I need to do a callback-like function?

I am currently trying to create an autocomplete with a source that is stored in a javascript variable but this variable can be updated by another function. So, what I would like is that at each time the user updates the autocomplete field, the source field of autocomplete is generated.

Here is the code I use:

<head>
    <script>
        var availableTags = ['java', 'javascript']
        // can be called anytime
        var addToTags = function(str){availableTags.push(str)}

        $(function() {
            $( "#tags" ).autocomplete({
                source: availableTags
            });
        });
    </script>
</head>
<body>
    <div class="ui-widget">
        <label for="tags">Tags: </label>
        <input id="tags" />
    </div>
</body>

Do I need to do a callback-like function?

Share Improve this question edited Dec 13, 2012 at 11:23 Arash Milani 6,3082 gold badges45 silver badges49 bronze badges asked Dec 13, 2012 at 9:10 Christopher ChicheChristopher Chiche 15.3k9 gold badges63 silver badges100 bronze badges 10
  • You should use $(document).ready(). – Mohammed H Commented Dec 13, 2012 at 9:11
  • What is sources? And: does it work currently or not? – Bergi Commented Dec 13, 2012 at 9:12
  • 1 I think your source needs to be a function, rather than an array. api.jqueryui.com/autocomplete/#option-source – hyankov Commented Dec 13, 2012 at 9:13
  • 1 @Bergi I corrected the code in the question that line should be var addToTags = function(str){availableTags.push(str)}. you can pair review it. – Arash Milani Commented Dec 13, 2012 at 9:18
  • 1 @ArashMilani Thanks a lot for the edit, it is exactly what I wanted (copy-paste error). – Christopher Chiche Commented Dec 13, 2012 at 10:09
 |  Show 5 more comments

3 Answers 3

Reset to default 13

a source that is stored in a javascript variable but this variable can be updated by another function.

That should just work. If both the autocomplete and the updating function reference the same array, you can push new values at any time which will be used as soon as the array is evaluated next time (e.g. on keystroke).

I would like that at each time the user updates the autocomplete field, the source field of autocomplete is generated.

That's a different one. Yes, this needs a callback function to generate the source array dynamically, but that's simple. Have a look at the docs:

$( "#tags" ).autocomplete({
    source: function(request, resolve) {
        // fetch new values with request.term
        resolve(availableTags);
    }
});

Just add a reset call to auto-complete in you addToTags function:

var addToTags = function(str){
   availableTags.push(str);
   $( "#tags" ).autocomplete({
       source: availableTags
   });
}

this is very straight forward

$( "#tags" ).autocomplete('option', 'source', availableTags)

setting availableTags array wherever needed

发布评论

评论列表(0)

  1. 暂无评论