javascript 如何在具有多个值的 Bootstrap-select 中动态选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32388954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:11:54  来源:igfitidea点击:

How to select dynamically in Bootstrap-select with multiple values

javascriptjquerybootstrap-select

提问by jemz

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

如果我的值为 1、3、4,如何使用 jQuery在Bootstrap-select 中动态选择多个值?

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>

回答by David Stosik

Use Bootstrap-Select's valmethod:

使用 Bootstrap-Select 的val方法:

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

http://jsfiddle.net/a4bxnwws/

http://jsfiddle.net/a4bxnwws/

See Bootstrap-Select's documentation.

请参阅Bootstrap-Select 的文档

回答by Joy

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

您可以使用两种方法设置 select 元素的值。

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

对于第一个,你可以使用bootstrap-select插件提供的方法:selectpicker(就像上面的答案);

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

第二个是使用jquery的方法——触发器。如:

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

回答by G. Sagar

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