javascript - 如何设置预输入的选定值

标签 javascript php jquery twitter-bootstrap typeahead.js

如何在 typeahead 中设置默认值。 我在这里尝试了很多解决方案,但似乎没有一个有效。

tagApi.tagsManager("pushTag", 'deafulttag');

下面是我的完整代码。

$(document).ready(function() {
    var tagApi = $(".tm-input").tagsManager();
    jQuery(".typeahead").typeahead({
        name: 'job_skill',
        displayKey: 'category',
        source: function (query, process) {
            return $.get('ajaxpro.php', { query: query }, function (data) {
                data = $.parseJSON(data);
                return process(data);
            });
        },
        afterSelect :function (item){
            tagApi.tagsManager("pushTag", item);
        }
    });
});

预先感谢您的帮助。

最佳答案

您可以用值预先填充元素吗?

jQuery(".typeahead").typeahead('val',"test");

来源:Bootstrap typeahead - How to set the default value manually

编辑:

在您的示例中,您希望使用如下所示的值启动预输入,而不是重新初始化它:

$(document).ready(function() {
    var tagApi = $(".tm-input").tagsManager();
    jQuery(".typeahead").typeahead({
        name: 'job_skill',
        displayKey: 'category',
        val: 'test',
        source: function (query, process) {
            return $.get('ajaxpro.php', { query: query }, function (data) {
                data = $.parseJSON(data);
                return process(data);
            });
        },
        afterSelect :function (item){
            tagApi.tagsManager("pushTag", item);
        }
    });
});

关于javascript - 如何设置预输入的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48077863/

相关文章:

javascript - 无法更新 Angular 对象

javascript - 在 React 中有条件地设置事件类

javascript - jquery autocomeplete 插件不能使用数值?

javascript - 创建新用户时,仪表板上没有任何显示

javascript - 使 Canvas 线条显示

javascript - 删除在 CSS 文件中设置的 CSS 属性(不是内联 CSS)[jQuery]

php - mysql根据特定条件和列值进行分组

php - 如何在不在 PHP 中循环的情况下创建具有 x 个相同字符的字符串

javascript - 遍历 jquery json

internet-explorer - 在 Internet Explorer 上使用 Windows 身份验证通过 SSL 进行跨域 ajax 调用失败