2014-06-23 125 views
1

如何通过输入属性设置选择选项?是否可以通过输入属性设置选择选项?

这不起作用(Fiddle):

<input id="test" value="cat" options="cat,dog,snake" type="text"/> 

<script> 
    $('#test').selectize(); 
</script> 

有了上面的代码,我想猫的预选,而狗,可作为下拉选项蛇。猫在value是好的,但其他人通过options没有。有任何想法吗?我在这里错过了一些东西。

+1

这是你想要的http://jsfiddle.net/zELuw/10/? –

+0

不,我想要三个选项,但作为默认选择只有一个(在这个例子中它是猫)。我更新了问题描述。 –

回答

0

你必须手动完成。 Selectize并不十分清楚如何设定其选项,但I figured it out

function to_selectize_options(items) { 
    result = []; 
    for (index in items) { 
     item = items[index]; 
     result.push({ 
      text: item, 
      value: item 
     }); 
    } 
    return result; 
} 

$('#test').each(function() { 
    node = $(this) 
    raw_options = node.attr('options') 
    options = to_selectize_options(raw_options.split(',')) 
    node.selectize({options:options, delimiter: ','});  
}) 

Check it out

相关问题