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

How to set a default value in selectize.js?

本文关键字:设置 默认值 js selectize      更新时间:2023-09-26

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

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

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

但这对Selectize不起作用。我已经阅读了API文档,并尝试使用:

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

但它也不起作用。

我是一个新手,所以我可能在这里错过了什么,但不知道是什么。有什么想法吗?

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

由于它是元素的属性,您可以使用jQuery:中的[0]语法来处理元素

// initialize the selectize control
var $select = $('.country').selectize();
$select[0].selectize.setValue("US");

也许可以使用项。。

$('select').selectize({
items : [""], // initializing selected value to nothing
plugins: ['remove_button','restore_on_backspace']
});