11

How can I select dynamically in Bootstrap-select with multiple values, if my values are 1,3,4, using jQuery?

Here is my select:

 <select  id="myselect" name="myselect[]" multiple>
        <option value=""></option>
        <option value="1">red</option>
        <option value="2">orange</option>
        <option value="3">green</option>
        <option value="4">blue</option>
</select>
David Stosik
  • 879
  • 9
  • 17
jemz
  • 4,987
  • 18
  • 62
  • 102

6 Answers6

30

Use Bootstrap-Select's val method:

$('#myselect').selectpicker('val', [1,3,4]);

http://jsfiddle.net/a4bxnwws/

See Bootstrap-Select's documentation.

David Stosik
  • 879
  • 9
  • 17
3

If you use selectpicker class then

<select class="selectpicker" id="myselect" name="myselect[]" multiple>
    <option value=""></option>
    <option value="1">red</option>
    <option value="2">orange</option>
    <option value="3">green</option>
    <option value="4">blue</option>
</select>

And Your jquery code will be like below:

var select_items = ["1","3","4"];
$('#myselect').selectpicker('val', select_items);
RokiDGupta
  • 371
  • 2
  • 7
  • 14
2

You can set the value for the select element using two methods.

For the first one, you can use the method that the bootstrap-select plugin provided: selectpicker(just like the above answer);

The second one is using jquery's method - trigger. Such as:

$('#myselect').val([1,3,4]).trigger('change');
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Joy
  • 31
  • 2
0

This should work:

$('#MySelectionBox').val(123).change();
Shoukat Mirza
  • 800
  • 9
  • 19
0
<form action="yourpage.php" method="post">
    <select  id="myselect" name="myselect[]" multiple>
        <option value=""></option>
        <option value="1">red</option>
        <option value="2">orange</option>
        <option value="3">green</option>
        <option value="4">blue</option>
    </select>
   <input type="submit" name="Submit" value="Submit">
</from>

PHP code starts to catch multiple selected values:

<?php
    if(isset($_POST['Submit']))
    {
      $MyValues = $_POST['myselect'];         
      foreach($MyValues As $value)
        {           
          echo $value."</br>";
        }
    }
?>
-3
// Check this code in detail

<form action="yourpage.php" method="post">
<select  id="myselect" name="myselect[]" multiple>
        <option value=""></option>
        <option value="1">red</option>
        <option value="2">orange</option>
        <option value="3">green</option>
        <option value="4">blue</option>
</select>
<input type="submit" name="Submit" value="Submit">
</from>

PHP code starts to catch multiple selected values:
<?php
if(isset($_POST['Submit']))
{
            $MyValues = $_POST['myselect'];         
            foreach($MyValues As $key => $value)
            {           
                $SID = mysqli_real_escape_string($dbCon,$value);
                echo $SID ."</br>";
             }
 }
?>
G. Sagar
  • 5
  • 4
  • 1
    Hi, Welcome to Stack Overflow and thank you for your answer. To make it super useful to others in the future, please can you consider adding some explanation to your answer that explains why it solves the OP's question, Thanks – Spangen May 25 '18 at 07:35