最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - selectpicker with select all by default - Stack Overflow

programmeradmin1浏览0评论

I am using the selectpicker plugin. Now I am trying to select all the options by default, or at least a button to select all without the need of click in the dropdown.

Currently the demo only works if the dropdown is clicked and after that, click the button.

Any idea?

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


$(".btn_clk").click(function () {
    $('.selectpicker').selectpicker('selectAll');
});

/

I am using the selectpicker plugin. Now I am trying to select all the options by default, or at least a button to select all without the need of click in the dropdown.

Currently the demo only works if the dropdown is clicked and after that, click the button.

Any idea?

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


$(".btn_clk").click(function () {
    $('.selectpicker').selectpicker('selectAll');
});

http://jsfiddle.net/tpnw96ed/

Share Improve this question asked Dec 15, 2014 at 15:53 user2990084user2990084 2,8409 gold badges33 silver badges47 bronze badges 1
  • Apparently this has been raised as a bug already: github.com/silviomoreto/bootstrap-select/issues/721 – athms Commented Dec 15, 2014 at 16:04
Add a comment  | 

4 Answers 4

Reset to default 8

There is default way to add Select All and Deselect All buttons to selectpicker now. You should use option data-actions-box="true" to enable buttons and data-select-all-text="Select all buttton name" data-deselect-all-text="Deselect all button name" to rename them

You can open/close the Selectpicker before/after clicking the button:

$(".btn_clk").click(function () {
    $('.dropdown-menu').css("display","block");
    $('.selectpicker').selectpicker('selectAll');
    $('.dropdown-menu').css("display","none");
});

Edit: You will also need to add the following to manually trigger the dropdown on clicks after a selectAll:

$('.selectpicker').click(function () {
    if($('.dropdown-menu').css("display") == "block") $('.dropdown-menu').css("display","none");
    else $('.dropdown-menu').css("display","block");
});

JSFIDDLE

Try this:

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

The solution in this thread works for me.

Here is the updated code of the plugin:

Line 888 and 895 in boostrapt-select.js

selectAll: function () {
  this.findLis();
  this.$lis.not('.divider').not('.disabled').not('.selected').not('.hide').find('a').click();
},

deselectAll: function () {
  this.findLis();
  this.$lis.not('.divider').not('.disabled').filter('.selected').not('.hide').find('a').click();
},

Note the change .filter(':visible') is now .not('.hide')

发布评论

评论列表(0)

  1. 暂无评论