How to set selected value on select using selectpicker plugin from bootstrap

前端 未结 18 2243
日久生厌
日久生厌 2020-11-27 13:29

I\'m using the Bootstrap-Select plugin like this:

HTML:


                        
    
提交评论

  • 2020-11-27 14:02

    Also, you can just call this for multi-value

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

    You can find more here https://developer.snapappointments.com/bootstrap-select/methods/

    0 讨论(0)
  • 2020-11-27 14:03

    You can set the selected value by calling the val method on the element.

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

    This will select all items in a multi-select.

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

    details are available on site : https://silviomoreto.github.io/bootstrap-select/methods/

    0 讨论(0)
  • 2020-11-27 14:04

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

    in place of selector you can give you selector either class or id for example: $('#mySelect').selectpicker('val',your_value)

    0 讨论(0)
  • 2020-11-27 14:04
    $('.selectpicker').selectpicker('val', '0');
    

    With the '0' being the value of the item you want selected

    0 讨论(0)
  • 2020-11-27 14:08

    Actually your value is set, but your selectpicker is not refreshed

    As you can read from documentation
    https://silviomoreto.github.io/bootstrap-select/methods/#selectpickerval

    The right way to do this would be

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

    For multiple values you can add array of values

    $('.selectpicker').selectpicker('val', [1 , 2]);
    
    0 讨论(0)
  • 提交回复
    热议问题