Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何使用引导中的selectpicker插件在select上设置selected值_Jquery_Twitter Bootstrap_Bootstrap Select - Fatal编程技术网

Jquery 如何使用引导中的selectpicker插件在select上设置selected值

Jquery 如何使用引导中的selectpicker插件在select上设置selected值,jquery,twitter-bootstrap,bootstrap-select,Jquery,Twitter Bootstrap,Bootstrap Select,我使用的插件如下: //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

我使用的插件如下:

//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);
HTML:

瓦尔1 Val 2 Val 3 Val 4 Javascript:

$'select[name=selValue]'。选择器; 现在,我想将单击按钮时选择的值设置为此选择。。。大概是这样的:

//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);
$'mybutton'。单击函数{ $'select[name=selValue]'; }; 但什么也没发生


如何实现这一点?

该值已正确选择,但您没有看到它,因为插件隐藏了真正的select并显示了一个无序列表的按钮,因此,如果您希望用户在select上看到所选值,可以执行以下操作:

//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: 要选择多个值,请将值作为数组传递

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

这对我很有用。

使用这个,它就会起作用:

 $('select[name=selValue]').selectpicker('val', 1);
这就是你需要做的一切。无需直接更改selectpicker生成的html,只需更改隐藏的selectpicker字段,然后调用selectpicker“刷新”。

当您没有硬编码的选项值(如1、2、3、4等)时,blushrt的回答略有变化

假设您呈现的选项值是某些用户的guid。然后,您需要以某种方式提取该选项的值,以便将其设置为

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

HTML:

丹尼斯 罗伯特 乔治 艾芬豪 Javascript:

//初始化选择选择器。 $'select[name=selValue]'。选择器; //提取第一个选项的值。 var sVal=$选择[name=selValue]选项:first'.val; //设置选定的值。 $'select[name=selValue]'; //强制刷新。 $'select[name=selValue]'。选择器'refresh';
我们在带有multiple关键字的select中使用了这个选项。在这种情况下,默认情况下不选择任何内容,但我们希望始终选择第一项。

如果使用val参数设置值,则无需刷新。对值使用括号可启用多个选定值

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

元素的用户ID,然后

document.getElementById('selValue').value=Your Value;
$('#selValue').selectpicker('refresh');

实际上,您的值已设置,但选择器不会刷新

正如您可以从文档中看到的那样

正确的方法是

$('.selectpicker').selectpicker('val', 1);
对于多个值,可以添加值数组

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

“0”是您要选择的项目的值,另一种方法是,使用此关键字,您可以简单地获取其中的对象并操纵其值。例如:

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

可以通过调用元素上的val方法来设置所选值

$('.selectpicker').selectpicker('val', 'Mustard');
$('.selectpicker').selectpicker('val', ['Mustard','Relish']);
这将选择多选中的所有项目

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

有关详细信息,请访问网站:

此外,您只需调用此函数即可获得多值

$。选择Pickerval,[value1,value2]


根据@blushrt的精彩回答,您可以在这里找到更多信息,我将更新此回复。 仅使用-

$("#Select_ID").val(id);
如果您已将所需的所有内容预加载到选择器中,则可以使用

$('.selectpicker option:selected').val();
Just put option:选中以获取值,因为引导选择器将更改为并以不同的方式显示。但仍选择已选择的

$“选择器”。选择器'val',值

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

代替选择器,您可以为选择器提供class或id,例如:$'mySelect'.selectpicker'val',您的_值

我不确定您想要实现什么,一旦用户单击selectYes中的该选项,就会设置该值,因为该值实际上来自ajax调用中的方法…该值被正确选择,但是你没有看到它,因为插件隐藏了真正的选择并显示了一个无序列表的按钮是的,我知道,但如何修复它。。。我是说。。。用户必须看到此控件上的选择更好的解决方案是:$'select[name=selValue]'。val1;$'。selectpicker'.selectpicker'refresh';通过这种方式,您只需更改隐藏的选择,调用selectpicker“refresh”会重新绘制按钮。是的,这种方式更好@blushrt,添加到我的回答中。我面临的问题是,我无法选择用户私自选择的预选值。是否需要在选择后刷新selectpicker@TomSarduy@ankitsuthar,是的,这应该是正确的答案!调用.selectpicker'refresh'命令对于选择和更新由selectpicker驱动的下拉列表的当前值至关重要。请注意,调用所有的刷新。selectpicker元素可能会很慢。如果您知道要操作的对象-最好在单个选择列表上调用refresh。$'.selectpicker'.selectpicker'refresh';如果一个页面上有很多选择器,则可能会降低性能我发现的另一个问题是,这不会勾选所选内容。我已尝试了您的解决方案。。但它在我的选择中显示为“nothing selected”,尽管我已经选择了值,并且我从存储数据的资源中得到了相同的值。我已经尝试了您的解决方案。。但它在我的选择中显示“nothing selected”,尽管我已经选择了值,并且我从存储数据的资源中得到了相同的值。Abd ,我有一个php脚本,它将json格式传递给ajax,其中json格式如下:car:8,颜色:红、蓝、白}。在这种情况下,如何使用此方法$'.selectpicker'.selectpickerval,data.colors;,在selectpicker中插入选中的对象颜色;?
$('.selectpicker').selectpicker("val", "value");