如何为selectize.jsinput设置一个值?

我有一个表单,我想复制一些默认值到input。 表单input使用selectize.js插件。 我想以编程方式设置一些表单值。 标准的做法是:

$("#my_input").val("My Default Value"); 

不起作用。

我已经尝试过这样的事情,但也不pipe用。

 var $select = $("#my_input").selectize(); var selectize = $select[0].selectize; selectize.setValue("My Default Value"); 

有任何想法吗? 这很容易:)我错过了。

检查API文档

方法addOption(data)setValue(value)可能是你正在寻找的东西。


更新 :看到这个答案的普及,这里是一些额外的信息根据意见/请求…

setValue(value, silent)
将所选项目重设为给定值。
如果“ 沉默 ”是真实​​的(即: true1 ),则不会在原始input上触发更改事件。

addOption(data)
添加一个可用的选项或一系列选项。 如果它已经存在,什么都不会发生。
注意:这不会刷新选项列表下拉列表(使用refreshOptions() )。


为了响应选项被覆盖:
这可以通过重新初始化select而不使用最初提供的选项来实现。 如果您不打算重新创build元素,只需将select对象存储到一个variables中即可:

 // 1. Only set the below variables once (ideally) var $select = $('select').selectize(options); // This initializes the selectize control var selectize = $select[0].selectize; // This stores the selectize object to a variable (with name 'selectize') // 2. Access the selectize object with methods later, for ex: selectize.addOption(data); selectize.setValue('something', false); // Side note: // You can set a variable to the previous options with var old_options = selectize.settings; // If you intend on calling $('select').selectize(old_options) or something 

这适用于我:

 var $select = $("#my_input").selectize(); var selectize = $select[0].selectize; selectize.setValue(selectize.search("My Default Value").items[0].id); 

但是你必须真的确定你只有一场比赛。

更新 :因为这个解决scheme完美的工作,即使在页面上有多个select元素,为了使其工作,我已经更新了答案:

以下方式我们可以初始化:

 $('select').each(function (idx) { var selectizeInput = $(this).selectize(options); $(this).data('selectize', selectizeInput[0].selectize); }); 

设定值务实后初始化:

 var selectElement = $('#unique_selector').eq(0); var selectize = selectElement.data('selectize'); if (!!selectize) selectize.setValue("My Default Value"); 

用户'onlyblank'的答案是正确的。 除此之外,您可以设置超过1个默认值。

而不是传递id到setValue(),传递一个数组。 例:

 var $select = $("#my_input").selectize(); var selectize = $select[0].selectize; var yourDefaultIds = [1,2]; # find the ids using search as shown by the user onlyblank selectize.setValue(defaultValueIds); 

只是遇到了同样的问题,并用下面这行代码解决了这个问题:

 selectize.addOption({text: "My Default Value", value: "My Default Value"}); selectize.setValue("My Default Value"); 

我有这个相同的问题 – 我正在使用Selectize与Rails,并希望select一个关联字段 – 我想相关的logging的名称显示在下拉列表中,但我需要每个选项的值是logging,因为Rails使用该value来设置关联。

我通过将@valueAttr的coffeescript var设置为每个对象的id ,将var的@dataAttr设置为该logging的name来解决此问题。 然后,我通过每个选项,并设置:

  opts.labelField = @dataAttr opts.valueField = @valueAttr 

它有助于看到完整的差异: https : //github.com/18F/C2/pull/912/files

注意setValue只有在已经为selectize控件(ex。select字段)预定义了一组值的情况下才有效,对于值可能是dynamic的控件(例如,用户可以随时添加值,textbox字段) setValue不会工作。

使用createItem functon代替添加和设置select字段的当前值

恩。

 $selectize[ 0 ].selectize.clear(); // Clear existing entries for ( var i = 0 ; i < data_source.length ; i++ ) // data_source is a dynamic source of data $selectize[ 0 ].selectize.createItem( data_source[ i ] , false ); // false means don't trigger dropdown 

这是我使用远程search标签的完整代码。 希望这是有帮助的。

 $('#myInput').selectize({ valueField: 'id', labelField: 'name', searchField: 'name', options: [], delimiter: ',', persist: false, create: false, load: function(query, callback) { if (!query.length) return callback(); $.ajax({ url: '/api/all_cities.php', type: 'GET', dataType: 'json', data: { name: query, }, error: function() { callback(); }, success: function(res) { callback(res); } }); }, onInitialize: function(){ var selectize = this; $.get("/api/selected_cities.php", function( data ) { selectize.addOption(data); // This is will add to option var selected_items = []; $.each(data, function( i, obj) { selected_items.push(obj.id); }); selectize.setValue(selected_items); //this will set option values as default }); } }); 

首先加载select下拉菜单,然后select它。 它将使用正常的$(select).val()。