问题
I am having a multiselect dropdown using the Boostrap Multiselect plugin (http://davidstutz.de/bootstrap-multiselect/) as below
<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>
On load of page, i will get an array of value like [101,102]. I should iterate through the array and make the values selected(check boxes corresponding to the ids should be checked). Please help.
回答1:
//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");
回答2:
Thank you all for your answers.
Taking all of your answers as a guideline, I resolved the problem with the below code:
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");
}
Thanks once again for all your support.
回答3:
It's supposed to be as simple as this:
<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>
Check my Fiddle: https://jsfiddle.net/luthrayatin/jaLygLzo/
回答4:
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);
}
Here is a demo
回答5:
work fine for me ,change ids according to you requirement.
$("#FormId select#status_id_new").val('');
$("#FormId select#status_id_new").multiselect("refresh");
回答6:
that code should help:
var selected=[101,103];
var obj=$('#data');
for (var i in selected) {
var val=selected[i];
console.log(val);
obj.find('option:[value='+val+']').attr('selected',1);
}
回答7:
from a php array into the multiselect...
$PHP_array=array(); // create array anyway you need to in PHP
array_push($PHP_array,20); // Single ID values
array_push($PHP_array,22);
$javascript_str = json_encode($PHP_array); // JSON ENCODE
// then in the JS script
$("#multiselectbox").val(<?php echo $javascript_str; ?>);
// Then refresh
$("#multiselectbox").multiselect("refresh");
回答8:
var valArr = ["1","2","3"];
/* Below Code Matches current object's (i.e. option) value with the array values */
/* Returns -1 if match not found */
$("select").multiselect("widget").find(":checkbox").each(function(){
if(jQuery.inArray(this.value, valArr) !=-1)
this.click();
});
This selects the options whose value matches with values in array.
回答9:
The official site has mentioned (in "Manually check or uncheck a checkbox?" section) just to use click()
e.g.
$("#ddl_singleSelect")
.multiselect("widget")
.find(":radio[value='']").click(); // change displayed selectedText
$("#ddl_singleSelect option[value='']")
.prop("selected", true) // change val()
.trigger("change"); // fire change event
$("#ddl_multiselect").multiselect("uncheckAll"); // init by de-selecting all
$("#ddl_multiselect")
.multiselect("widget")
.find( ":checkbox[value='1']"
+ ",:checkbox[value='2']"
+ ",:checkbox[value='3']"
+ ",:checkbox[value='4']"
).click(); // change displayed selecetdText
$("#ddl_multiselect")
.find( "option[value='1']"
+ ",option[value='2']"
+ ",option[value='3']"
+ ",option[value='4']"
).prop("selected", true); // change val()
$("#ddl_multiselect").trigger("change"); // fire change event
OR by HTML
<select name="example-presets" multiple="multiple" size="5">
<option value="option1" selected="selected">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3" selected="selected">Option 3</option>
<option value="option4" selected="selected">Option 4</option>
<option value="option5" disabled="disabled">Option 5</option>
<option value="option6" disabled="disabled">Option 6</option>
<option value="option7">Option 7</option>
<option value="option8">Option 8</option>
<option value="option9">Option 9</option>
</select>
....
<script type="text/javascript">$("select").multiselect();</script>
回答10:
I got a better solution from jquery-multiselect documentation.
var data = [101,102];
$("#data").multiSelect('deselect_all');
$("#data").multiSelect("select",data);
回答11:
$("#dropDownId").val(["130860","130858"]);
$("#dropDownId").selectmenu('refresh');
回答12:
this is my code sample. I have comma separated numbers for multi select drop down and want to select options multiple options from comma separated values.
var selected_tags_arr = new Array();
var selected_tags = 1,2,4;
selected_tags_arr = selected_tags.split(",");
$('#contact_tags option').each(function (){
var option_val = this.value;
for (var i in selected_tags_arr) {
if(selected_tags_arr[i] == option_val){
$("#contact_tags option[value='" + this.value + "']").attr("selected", 1);
}
}
});
回答13:
This is what worked from me using an array as input. First uncheck all options, then click on the chosen options using jquery
var dimensions = ["Dates","Campaigns","Placements"];
$(".input-dimensions").multiselect("uncheckAll");
for( var dim in dimensions) {
$(".input-dimensions").multiselect("widget")
.find(":checkbox[value='" + dimensions[dim] + "']").click();
}
回答14:
I ended up having to make a slight change using the click event on the input element by also setting the checked prop after firing the click event.
$(y.Ctrl).multiselect("widget").find(":checkbox").each(function () {
if ($.inArray(this.value, uniqueVals) != -1) {
$(this).click();
$(this).prop('checked', true);
}
});
回答15:
I hope these functions will help you.
$('#your-select').multiSelect('select', String|Array);
$('#your-select').multiSelect('deselect', String|Array);
$('#your-select').multiSelect('select_all');
$('#your-select').multiSelect('deselect_all');
$('#your-select').multiSelect('refresh');
Reference by this site Reference
回答16:
You can try this:
$( "#data" ).val([ "100", "101" ]);
来源:https://stackoverflow.com/questions/6966260/jquery-bootstrap-multiselect-plugin-set-a-value-as-selected-in-the-multiselect