javascript – jQuery自动完成是否使用动态数组作为源

前端之家收集整理的这篇文章主要介绍了javascript – jQuery自动完成是否使用动态数组作为源前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在尝试使用存储在 javascript变量中的源创建自动完成,但此变量可以由另一个函数更新.所以,我想要的是,每次用户更新自动完成字段时,都会生成自动完成的源字段.

这是我使用的代码

<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>

我是否需要执行类似回调的功能

解决方法

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

这应该工作.如果自动完成和更新功能都引用相同的数组,您可以随时按下新值,这将在下次评估数组时使用(例如,在按键上).

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

那是另一回事.是的,这需要一个回调函数来动态生成源数组,但这很简单.看看the docs

$( "#tags" ).autocomplete({
    source: function(request,resolve) {
        // fetch new values with request.term
        resolve(availableTags);
    }
});
原文链接:https://www.f2er.com/jquery/159423.html

猜你在找的jQuery相关文章