很舒服的表示方式,json

[ 1577 查看 / 5 回复 ]


 附件: 您所在的用户组无法下载或查看附件
TOP

jason,从服务器端去取 整型、浮点型
TOP

{"randomFloat":0.38160862465463979,"randomArray":[606809764,1060503456,1808187865]}
TOP

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title></title>
  5.     <script type="text/javascript">
  6.         window.onload = function () {

  7.             var ds = DataSource();
  8.             //    alert(ds.length);
  9.             var p = $("province");

  10.             for (var i = 0; i < ds.length; i++) {
  11.                 var province = ds[i];
  12.                 var p_option = document.createElement("option");
  13.                 p_option.value = province.PID;
  14.                 p_option.text = province.Name;
  15.                 p.options.add(p_option);
  16.             }


  17.             p.onchange = function () {
  18.                 var index = p.selectedIndex;
  19.                 var value = p.value;
  20.                 var count = p.options.length;

  21.                 //                output(index);
  22.                 //                output(value);
  23.                 //                output(count);
  24.                 var citys = $("city");
  25.                 citys.options.length = 0;
  26.                 var emp = document.createElement("option");
  27.                 emp.text = "-----请选择城市-------";
  28.                 citys.options.add(emp);
  29.                 //                if (index == 1) {

  30.                 //                    var op = document.createElement("option");
  31.                 //                    op.value = "baoshan";
  32.                 //                    op.text = "宝山";


  33.                 //                    citys.options.add(op);
  34.                 //                }

  35.                 var ds = DataSource();
  36.                 for (var i = 0; i < ds.length; i++) {
  37.                     var province = ds[i];
  38.                     if (province.PID == value) {
  39.                         var citys_array = province.Citys;

  40.                         for (var c = 0; c < citys_array.length; c++) {
  41.                             var city = citys_array[c];

  42.                             var c_option = document.createElement("option");
  43.                             c_option.value = city.CID;
  44.                             c_option.text = city.Name;
  45.                             citys.options.add(c_option);
  46.                         }
  47.                     }

  48.                 }


  49.                 citys.onchange = function () {
  50.                     if (citys.selectedIndex > 0) {
  51.                     var op = citys.options[citys.selectedIndex];

  52.                         output(op.value + ":" + op.text);
  53.                     }
  54.                 };
  55.             };
  56.         };


  57.         function DataSource() {
  58.             var ds = [

  59.             {
  60.                 PID: "shanghai",
  61.                 Name: "上海",

  62.                 Citys: [
  63.                     {
  64.                         CID: "baoshan",
  65.                         Name: "宝山"
  66.                     },
  67.             {
  68.                 CID: "xinzhuang",
  69.                 Name: "辛庄"
  70.             }
  71.                     ]
  72.             },
  73.             {
  74.                 PID: "shandong",
  75.                 Name: "山东",

  76.                 Citys: [
  77.                     {
  78.                         CID: "taian",
  79.                         Name: "泰安"
  80.                     },
  81.             {
  82.                 CID: "jinan",
  83.                 Name: "济南"
  84.             }
  85.                     ]
  86.             }

  87.             ];
  88.             return ds;
  89.         }


  90.         function $(id) {
  91.             return document.getElementById(id);
  92.         }
  93.         function output(txt) {

  94.             document.getElementById("result").innerHTML += txt + "<br/>";
  95.         }
  96.     </script>
  97. </head>
  98. <body>
  99.     <select id="province">
  100.         <option>-----请选择省------</option>
  101.         <!--
  102.         <option value="shanghai">上海</option>
  103.         <option value="shandong">山东</option>
  104.         <option value="shanxin">山西</option>
  105.         <option value="jiangsu">江苏</option>
  106.         <option value="fujian">福建</option>
  107.         <option value="henan">河南</option>-->
  108.     </select>
  109.     <select id="city">
  110.         <option>-----请选择城市------</option>
  111.     </select>
  112.     <div id="result">
  113.     </div>
  114. </body>
  115. </html>
复制代码
TOP

正巧,今天上javascript也用到json来实现二级联动
TOP