Pri*_*nce 52 jquery bootstrap-multiselect jquery-ui-multiselect
我有一个多选下拉列表,如下所示
<select id="data" name="data" class="data" multiple="multiple">
<option value="100">foo</option>
<option value="101">bar</option>
<option value="102">bat</option>
<option value="103">baz</option>
</select>
Run Code Online (Sandbox Code Playgroud)
在加载页面时,我将得到一个像[101,102]这样的值数组.我应该遍历数组并选择值(应检查对应于id的复选框).请帮忙.
小智 144
//Do it simple
var data="1,2,3,4";
//Make an array
var dataarray=data.split(",");
// Set the value
$("#multiselectbox").val(dataarray);
// Then refresh
$("#multiselectbox").multiselect("refresh");
Run Code Online (Sandbox Code Playgroud)
Pri*_*nce 24
谢谢大家的答案.
以您的所有答案为准则,我使用以下代码解决了问题:
var valArr = [101,102];
i = 0, size = valArr.length;
for(i; i < size; i++){
$("#data").multiselect("widget").find(":checkbox[value='"+valArr[i]+"']").attr("checked","checked");
$("#data option[value='" + valArr[i] + "']").attr("selected", 1);
$("#data").multiselect("refresh");
}
Run Code Online (Sandbox Code Playgroud)
再次感谢您的支持.
Yat*_*tin 19
它应该像这样简单:
<select id='multipleSelect' multiple='multiple'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<script type='text/javascript'>
$('#multipleSelect').val(['1', '2']);
</script>
Run Code Online (Sandbox Code Playgroud)
检查我的小提琴:https://jsfiddle.net/luthrayatin/jaLygLzo/
She*_*hef 10
var valArr = [101,102], // array of option values
i = 0, size = valArr.length, // index and array size declared here to avoid overhead
$options = $('#data option'); // options cached here to avoid overhead of fetching inside loop
// run the loop only for the given values
for(i; i < size; i++){
// filter the options with the specific value and select them
$options.filter('[value="'+valArr[i]+'"]').prop('selected', true);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
对我来说工作正常,请根据您的要求更改ID。
$("#FormId select#status_id_new").val('');
$("#FormId select#status_id_new").multiselect("refresh");
Run Code Online (Sandbox Code Playgroud)