11.Layui后台异步加载select下拉菜单

前端html页面

    <label class="layui-form-label">服务器环境</label>
    <div class="layui-input-inline" style="width:150px;">
        <select name="server" id="server">
            <option value="">请选择服务器</option>
        </select>
    </div>
</div>

js脚本

$.ajax({
    url:"/selectServer",
    type:"GET",
    dataType:"json",
    success:function(result){
        var list = result;    //返回的数据
        var server = document.getElementById("server"); //server为select定义的id
        for(var p in list){
            var option = document.createElement("option");  // 创建添加option属性
            option.setAttribute("value",p); // 给option的value添加值
            option.innerText=list[p];     // 打印option对应的纯文本 
            server.appendChild(option);           //给select添加option子标签
            form.render("select");            // 刷性select,显示出数据
} } });}

后端controller返回JSONObject给前端作显示处理,如:{"xx":"aaa","yy":"bbb"}

@RequestMapping("/selectServer")
    public JSONObject selectServer(HttpServletRequest request){
        List<Config> configContent = new ArrayList<Config>();
        configContent = configService.getConfigList(null,9,null);
        JSONObject ServerJson = new JSONObject();
        for (Config c : configContent) {
            ServerJson.put(c.getConfigkey(), c.getConfigContent());
        }
        log.info("request:selectServer, response:" + ServerJson);
        return ServerJson;
    }

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值