How to select dynamically in Bootstrap-select with multiple values

大城市里の小女人 提交于 2019-12-10 13:13:47

问题


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>

回答1:


Use Bootstrap-Select's val method:

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

http://jsfiddle.net/a4bxnwws/

See Bootstrap-Select's documentation.




回答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');



回答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>";
             }
 }
?>


来源:https://stackoverflow.com/questions/32388954/how-to-select-dynamically-in-bootstrap-select-with-multiple-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!