javascript 以编程方式将项目添加到输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20458585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:32:33  来源:igfitidea点击:

Add item to input programmatically

javascriptjquerytaggingselectize.js

提问by daGrevis

Selectize.jsallows to transform inputs into widgets with tagging, auto-complete etc.. I'm trying to add tag into input using code.

Selectize.js允许将输入转换为带有标记、自动完成等的小部件。我正在尝试使用代码将标记添加到输入中。

Here's what I have so far.

这是我到目前为止所拥有的。

$(function() {
    $("#tags").selectize({
        create: true
    })

    var selectize_tags = $("#tags")[0].selectize
    selectize_tags.createItem("foo")
    selectize_tags.refreshItems()
})

http://jsfiddle.net/qDL37/

http://jsfiddle.net/qDL37/

Unfortunately, “foobar“ isn't added to input box. As far as I know, it's the correct wayto do it.

不幸的是,“foobar”没有添加到输入框中。据我所知,这是正确的方法

Could this be a bug in selectize.js? I tried to search through GitHub issues, but couldn't find anything like that.

这可能是 selectize.js 中的错误吗?我试图搜索 GitHub 问题,但找不到类似的东西。

Also I tried to read code of selectize.js, but no luck.

我也尝试阅读 selectize.js 的代码,但没有运气。

回答by daGrevis

Thanks to great people from #javascript @freenode, this is the correct way.

感谢来自#javascript @freenode 的伟大人士,这是正确的方法。

$(function() {
    $("#tags").selectize({
        create: true
    })

    var selectize_tags = $("#tags")[0].selectize
    selectize_tags.addOption({
        text:'Foo',
        value: 'foo'
    });
    selectize_tags.addItem('foo')
    // selectize_tags.refreshItems()
})

http://jsfiddle.net/qDL37/1/

http://jsfiddle.net/qDL37/1/