我正在使用Bootstrap Select并使用以下内容:
1)我的用户json数组看起来像: [{“usr”:1,“name”:“Bob”},{“usr”:3,“name”:“Janet”},{“usr”:6,“name”:“Perry”} ..
我没有选择Mustard,Ketchup和Relish,而是想从json数组动态创建下拉列表,其中value = usr和text displayed = name。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Jquery & Bootstrap -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/css/bootstrap-select.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/js/bootstrap-select.min.js"></script>
<!-- (Optional) Latest compiled and minified JavaScript translation files
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/js/i18n/defaults-*.min.js"></script> -->
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
.selected {
background-color: lightblue;
color:#000;
text-shadow:0 1px 0 rgba(0, 0, 0, 0.4);
}
.dropdown-menu>li>a:hover {
background-color: lightblue!important;
background-image:none!important
}
</style>
<script type="text/javascript">
// global Javascript variables
users = <?php echo json_encode($users); ?>
</script>
</head>
<body>
<form action="select.php" method="POST">
<div style='margin:0 auto;text-align:center'>
<h1>Select Box!</h1>
<br>
<select name='crewNames[]' class="selectpicker" data-width="15%" multiple data-selected-text-format="count">
<option>Mustard</option>
<option>Ketchup</option>
<option>Relish</option>
</select>
<br><br>
<button type="submit" >Update</button>
</div>
</form>
</body>
<script>
$( document ).ready(function() {
$('.selectpicker').selectpicker('val', ['Mustard']);
});
</script>
</html>
答案 0 :(得分:3)
$(document).ready(function() {
users = <?php echo json_encode($users); ?>
users = JSON.parse(users);
$('#crew-names').empty();
$.each(users, function(index, element) {
var option = $('<option>').attr('value', element.usr).text(element.name);
$('#crew-names').append(option);
});
});
将crew-names
指定为下拉元素的ID,以便更容易选择和操作元素。