Ajax获取Json对象绑定下拉框

时间:2023-01-30 18:57:48

Html 部分:

<html>
<head></head>
<body>
<div class="form-group">
                <div class="col-sm-3">
                    <label class="control-label">集团:</label>
                </div>
                <div class="col-sm-9">
                    <select id="groupType" name="offsetType" class="table ">
                    </select>
                </div>
 </div>
</body>
</html>

 

Js部分:

$.ajax({
   type: "post",
   contentType: "application/x-www-form-urlencoded;charset=utf-8",
   url: rootPath + '/group/getGroupList.json',
   dataType: "json",
   data: "",
   success: function (response) {
      if (response.code == "0") {
         var ddl = $("#groupType");//列表框id
          //方法1:添加默认节点
         ddl.append("<option value='-1'>--请选择--</option>");
         //转成Json对象
         var result = eval(response.obj);
         //循环遍历 下拉框绑定
         $(result).each(function (key) {
         //第一种方法
         var opt = $("<option></option>").text(result[key].organizationName).val(result[key].organizationCode);
         ddl.append(opt);
         });
      } else {
         layer.msg('加载集团失败');
      }
   },
   error: function () {
      layer.msg('error');
   }
});