如何使用bootstrap中的selectpicker插件在select上设置所选值

时间:2013-02-11 00:21:20

标签: jquery twitter-bootstrap bootstrap-select

我正在使用Bootstrap-Select这样的插件:

HTML

<select name="selValue" class="selectpicker">
   <option value="1">Val 1</option>
   <option value="2">Val 2</option>
   <option value="3">Val 3</option>
   <option value="4">Val 4</option>
</select>

的Javascript

$('select[name=selValue]').selectpicker();

现在我想在点击按钮时将选择的值设置为此选择...如下所示:

$('#mybutton').click(function(){
   $('select[name=selValue]').val(1);
});

但没有任何反应。

我怎样才能做到这一点?

17 个答案:

答案 0 :(得分:119)

正确选择了该值,但您没有看到它,因为插件隐藏了真正的选择并显示了带有无序列表的按钮,因此,如果您希望用户在选择中看到所选值,您可以执行此操作像这样的东西:

//Get the text using the value of select
var text = $("select[name=selValue] option[value='1']").text();
//We need to show the text inside the span that the plugin show
$('.bootstrap-select .filter-option').text(text);
//Check the selected attribute for the real select
$('select[name=selValue]').val(1);

编辑:

就像@blushrt指出的那样,更好的解决方案是:

$('select[name=selValue]').val(1);
$('.selectpicker').selectpicker('refresh')

编辑2:

要选择多个值,请将值作为数组传递。

答案 1 :(得分:59)

$('select[name=selValue]').val(1);
$('.selectpicker').selectpicker('refresh');

这就是你需要做的。无需直接更改selectpicker生成的html,只需更改隐藏的选择字段,然后调用selectpicker('refresh')。

答案 2 :(得分:36)

$('.selectpicker').selectpicker('val', YOUR_VALUE);

答案 3 :(得分:15)

如果使用“val”参数设置值see fiddle,则无需刷新。使用括号作为值以启用多个选定值。

$('.selectpicker').selectpicker('val', [1]); 

答案 4 :(得分:9)

$('#mybutton').click(function(){
   $('select[name=selValue]').val(1);
   $('select[name=selValue]').change();
});

这对我有用。

答案 5 :(得分:4)

$('selector').selectpicker('val',value);

代替选择器,您可以为选择器提供类或ID,例如:$('#mySelect').selectpicker('val',your_value)

答案 6 :(得分:3)

您可以通过在元素上调用val方法来设置选定的值。

$('.selectpicker').selectpicker('val', 'Mustard');
$('.selectpicker').selectpicker('val', ['Mustard','Relish']);

这将选择所有项目。

$('.selectpicker').selectpicker('selectAll');

详细信息可在网站上找到:https://silviomoreto.github.io/bootstrap-select/methods/

答案 7 :(得分:2)

listview

答案 8 :(得分:1)

blushrt 答案的轻微变化,当您没有&#34;硬编码&#34;选项的价值(即1,2,3,4等)

假设您使用选项值作为某些用户的GUID呈现<select>。然后,您需要以某种方式提取选项的值,以便在<select>上设置它。

在下文中,我们选择select的第一个选项。

<强> HTML

<select name="selValue" class="selectpicker">
   <option value="CD23E546-9BD8-40FD-BD9A-3E2CBAD81A39">Dennis</option>
   <option value="4DDCC643-0DE2-4B78-8393-33A716E3AFF4">Robert</option>
   <option value="D3017807-86E2-4E56-9F28-961202FFF095">George</option>
   <option value="991C2782-971E-41F8-B532-32E005F6A349">Ivanhoe</option>
</select>

<强>的Javascript

// Initialize the select picker.
$('select[name=selValue]').selectpicker();

// Extract the value of the first option.
var sVal = $('select[name=selValue] option:first').val();

// Set the "selected" value of the <select>.
$('select[name=selValue]').val(sVal);

// Force a refresh.
$('select[name=selValue]').selectpicker('refresh');

我们在带有多个关键字<select multiple>的选择中使用了此选项。在这种情况下,默认情况下不会选择任何内容,但我们希望始终选择第一个项目。

答案 9 :(得分:0)

$('.selectpicker').selectpicker('val', '0');

“0”是您想要选择的项目的值

答案 10 :(得分:0)

另一种方法是,使用 关键字,您只需在 中获取对象并操纵它的价值。例如:

$("select[name=selValue]").click(
    function() {
        $(this).val(1);
    });

答案 11 :(得分:0)

此外,您可以将其称为多值

$(<your select picker>).selectpicker("val", ["value1", "value2"])

您可以在这里https://developer.snapappointments.com/bootstrap-select/methods/

中找到更多内容

答案 12 :(得分:0)

基于@blushrt的出色回答,我将更新此响应。 只需使用-

$("#Select_ID").val(id);

如果您已将所需的所有内容预加载到选择器,则可以使用。

答案 13 :(得分:0)

$('.selectpicker option:selected').val();

仅放置选项:选择该选项可获取值,因为引导选择器更改为并以不同的方式出现。但是选择仍然在那里选择

答案 14 :(得分:0)

$('.selectpicker').selectpicker("val", "value");

答案 15 :(得分:-1)

用户ID对于元素,则

writeLock

答案 16 :(得分:-3)

使用它,它会起作用:

 $('select[name=selValue]').selectpicker('val', 1);
相关问题