我正在尝试找到一种在 selectize.js 中设置默认值的方法。
我知道在 jQuery 中你可以使用:
$(".country").val("US");
但这不适用于 Selectize。我已经阅读了 API 文档并尝试使用:
$(".country").selectize().setValue("US");
但它也不起作用。
我是新手,所以我可能在这里遗漏了一些东西,但无法弄清楚是什么。有什么想法吗?
最佳答案
根据 documentation , 在 selectize() 调用添加到原始 select 或 input 元素的 selectize 属性上调用 API 函数;此属性指向底层的 selectize 实例。
因为它是元素的属性,所以您可以对 jQuery 中的元素使用 [0] 语法:
// initialize the selectize control
var $select = $('.country').selectize();
$select[0].selectize.setValue("US");
关于javascript - 如何在 selectize.js 中设置默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25870993/