doujiao3072 2016-11-30 22:13
浏览 132
已采纳

从json动态添加下拉列表

I'm using Bootstrap Select with the following:

1) my user json array looks like: [{"usr":1,"name":"Bob"},{"usr":3,"name":"Janet "},{"usr":6,"name":"Perry"}...

Instead of having the options Mustard, Ketchup and Relish I would like to dynamically create the drop down list from the json array where the value = usr and 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>
  • 写回答

1条回答 默认 最新

  • douguo7431 2016-11-30 22:29
    关注
    $(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);
        });
    });
    

    Assign crew-names as the ID to the dropdown element so it will be easier to select and manipulate the element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算