jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 如何设置选择值使用selectpicker插件从bootstrap大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用 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);
});

但没有发生。

我该如何实现呢?

解决方法

该值被正确选择,但是你没有看到它,因为插件隐藏真正的选择并显示一个无序列表的按钮,所以,如果你想让用户看到选择的值,你可以做这样的事情:
//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')

大佬总结

以上是大佬教程为你收集整理的jquery – 如何设置选择值使用selectpicker插件从bootstrap全部内容,希望文章能够帮你解决jquery – 如何设置选择值使用selectpicker插件从bootstrap所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。