zoukankan      html  css  js  c++  java
  • ajax获取json数据 for select2

    json数据“a.json”

    [
        {
            "id": "1", 
            "text": "张三"
        }, 
        {
            "id": "2", 
            "text": "李师师"
        }, 
        {
            "id": "3", 
            "text": "林冲"
        }, 
        {
            "id": "4", 
            "text": "周通"
        }, 
        {
            "id": "5", 
            "text": "杨康"
        }
    ]


    js获取json文件数据

    $.getJSON(url, function (data) {
            control.empty();//清空下拉框
            $.each(data, function (i, item) {
                //alert(item.id)
                control.append("<option value='" + item.id + "'>&nbsp;" + item.text + "</option>");
            });
        });


    html代码

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>select2</title>
      <!-- Tell the browser to be responsive to screen width -->
      <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
      <!-- Select2 -->
      <link rel="stylesheet" href="plugins/select2/select2.min.css">
    </head>
    <body>
    
      <select class="js" multiple="multiple">
        <option value="AL">Alabama</option>
        <option value="WY">Wyoming</option>
     </select>
      <!-- jQuery 2.1.4 -->
      <script src="http://cdn.hcharts.cn/jquery/jquery-2.1.4.min.js"></script>
      <!-- Select2 -->
      <script src="plugins/select2/select2.full.min.js"></script>
      <!-- SlimScroll 1.3.0 -->
      <script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
      <!-- page script -->
      <script src="js/data.js"></script>
    
      <script>    
        $(function () { 
          $(".js").select2();
          BindDictItem('js','a');
      });
      </script>
    </body>
    </html>
  • 相关阅读:
    面试题,找出每个产品的最新五个产品,还有其它方法吗 —— 游标加表变量
    SQL排序,重名和调名
    存储过程分页
    Perl/Python 感概
    Perl解析INI文件
    Perl 多进程文件锁
    Windows Sharepoint Services 版本更新
    工作、SOA、MBF...
    Windows Sharepoint Services 版本更新
    被CDOEXM折磨了一把
  • 原文地址:https://www.cnblogs.com/moiam/p/5103052.html
Copyright © 2011-2022 走看看