问题
How to selected Db values selected in select dropdown list like PHP in Multiselect boostrap dropdown?
For example Php coding,
$array = array("apple", "orange", "lemon");
<select>
<option value=""></option>
<?php
foreach ($array as $data) {
?>
<option value="<?php echo $data ?>" <?php if ($data == "orange") echo "selected"; ?>><?php echo $data ?></option>
<?php } ?>
In php db value select using Selected,How in Boostrap Multiselect js?
<button type="button" class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" title="None selected">
None selected
<b class="caret">
</b>
<ul class="multiselect-container dropdown-menu">
<li class="multiselect-item multiselect-all">
<a href="javascript:void(0);" class="multiselect-all">
<label class="checkbox">
<input type="checkbox" name="multiselect" value="multiselect-all">
Select all
</label>
</a>
</li>
<li>
<a href="javascript:void(0);">
<label class="checkbox">
<input type="checkbox" name="multiselect" value="cheese">
Cheese
</label>
</a>
</li>
How to set value in above boostrap multiselect js? like selected option in php?for example:if db value coming cheese means selected value in Multiselect Dropdown?
回答1:
You can do that by using the select
function of the plugin
$(document).ready(function() {
$('#example-getting-started').multiselect();
$('#example-getting-started').multiselect('select', ['cheese', 'pepperoni']);
});
jsbin
回答2:
From my understandingof your problem: You are using PHP to generate the select with its option and you retrieve the option and their selected/unselected state form a database. Your code snippet should be fine, Bootstrap Multiselect automatically preserves selected options (both in multiple and single mode). Note that in single mode (i.e. single selection through radio buttons) the last selected option is preselected in Bootstrap Multiselect, while in multiple mode all selected options will be preselected.
<?php $array = array("apple", "orange", "lemon"); ?>
<select> <!-- Note that the multiple attribute needs to be present to allow multiple selection. -->
<!-- Removed the empty option, not sure what it was for ... -->
<?php foreach ($array as $data): ?>
<option value="<?php echo $data ?>" <?php if ($data == "orange") echo "selected=\"selected\""; ?>><?php echo $data ?></option>
<?php endforeach; ?>
<!-- Do not forget closing tag. -->
</select>
<script type="javascript/text">
$(document).ready(function() {
// Will preselect "orange" in single selection mode ...
$('select').multiselect({
includeSelectAllOption: true
});
});
</script>
After page load, the multiselect can be manipulated as described by lcycool.
来源:https://stackoverflow.com/questions/38220649/how-to-select-box-value-selected-from-db-values-in-boostrap-multiselect-js