javascript 如何在 selectize.js 中设置默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25870993/
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-28 05:08:32  来源:igfitidea点击:

How to set a default value in selectize.js?

javascriptjqueryhtmlselectize.js

提问by Vivian Guillen

I'm trying to find a way to set a default value in selectize.js.

我试图找到一种在 selectize.js 中设置默认值的方法。

I know that in jQuery you can use:

我知道在 jQuery 中你可以使用:

$(".country").val("US");

But this doesnt work with Selectize. I already read the API documentation and I tried using:

但这不适用于 Selectize。我已经阅读了 API 文档并尝试使用:

$(".country").selectize().setValue("US");

But it doesnt work either.

但它也不起作用。

I'm a newbie so I'm probably missing something here but can't figure out what. Any ideas?

我是新手,所以我可能在这里遗漏了一些东西,但不知道是什么。有任何想法吗?

回答by Allan Bazinet

Per the documentation, API functions are called on the selectize property that the selectize() call adds to the original select or input element; this property points to the underlying selectize instance.

根据文档,在 selectize() 调用添加到原始选择或输入元素的 selectize 属性上调用 API 函数;这个属性指向底层的 selectize 实例。

Since it's a property of the element, you use the [0] syntax to the element from jQuery:

由于它是元素的属性,因此您可以对 jQuery 中的元素使用 [0] 语法:

// initialize the selectize control
var $select = $('.country').selectize();

$select[0].selectize.setValue("US");