2014-02-22 110 views
39

我有一個表單,我想將一些默認值複製到輸入中。表單輸入使用selectize.js插件。我想以編程方式設置一些表單值。這樣做的標準方法:如何設置selectize.js輸入的值?

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

不起作用。

我試過這樣的東西,但它也不起作用。

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

任何想法?這很容易:)我錯過了它。

回答

44

檢查API Docs

方法addOption(data)setValue(value)可能是你在找什麼。


更新:看到這個答案的普及,這裏是一個基於的意見/請求一些額外的信息...

setValue(value, silent)
 重置所選項目的給定值。
 如果「沉默」是truthy(即:true,1),沒有更改事件將觸發原始輸入。

addOption(data)
 添加可用選項或選項數組。如果它已經存在,什麼都不會發生。
 注意:這不會刷新選項列表下拉菜單(對此使用refreshOptions())。


爲了應對方案被改寫:
這可以通過重新初始化選擇不使用您最初提供的選項發生。如果您不打算重新創建元素,只需將selectize對象存儲到一個變量:

// 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 
+0

我看過的文檔,並試圖您的建議。看我的例子。看不行。當我找到答案時,我一定會踢自己。 –

+0

您的jQuery選擇器中沒有'#my_input'附近的引號。 – Tomanow

+0

感謝您的錯字。我在原始代碼中修正了它,並在複製到stackoverflow時錯過了它。好消息是,我所說的作品。我的實際表單中有一個錯誤是導致問題的原因。 –

24

這個工作對我來說:

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

,但是你必須真的真的確定你只有一場比賽。

更新:由於該解決方案可以完美運行,按順序進行它的工作即使有頁面上的多箇中選擇元素,我已經更新了答案:

下面的方式,我們可以初始化:

$('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"); 
+0

非常感謝@onlyblank –

+0

對我也有效,謝謝 – coder771

+0

感謝您的回答,但對我來說,它沒有像這樣搜索selectize.setValue(「我的默認值」);你也是這樣,我們必須確保價值不再重複。 – Harsha

2

我有同樣的問題 - 我使用的Selec使用Rails進行tize並希望選擇一個關聯字段 - 我希望相關記錄的名稱顯示在下拉列表中,但我需要每個選項的值作爲記錄的ID,因爲Rails使用value來設置關聯。

我通過設置@valueAttr一個CoffeeScript的VAR到每個對象的id@dataAttr到記錄的name一個VAR解決了這個。然後,我在每個選項便跑到:

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

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

6

受到了用戶的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); 
2

的setValue僅如果已經有組預定義的selectize控制值的工作,對控制其中的值可以是動態的(例如用戶可以添加值(例如選擇字段)。飛,文本框字段)setValue是不會工作。

使用createItem functon的,而不是添加和設置selectize場

前的當前值。

$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 
3

恰好碰到了同樣的問題,並與下面的代碼行解決了這個問題:

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

這是我使用的標籤從遠程搜索全部代碼。希望這是有幫助的。

$('#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 
    }); 
} 
}); 
0

首先加載選擇下拉菜單,然後選擇它。 它將與普通的$(select).val()一起工作。

0

我有一個類似的問題。我的數據由遠程服務器提供。我希望在選擇框中輸入一些值,但事先並不確定此值是否在服務器上有效。

所以我想要在框中輸入值,並且我希望選擇性顯示可能的選項,就像用戶輸入值一樣。

最後我用這個技巧(這很可能是不支持):

var $selectize = $("#my_input").selectize(/* settings with load*/); 
var selectize = $select[0].selectize; 
// get the search from the remote server 
selectize.onSearchChange('my value'); 
// enter the input in the input field 
$selectize.parent().find('input').val('my value'); 
// focus on the input field, to make the options visible 
$selectize.parent().find('input').focus();