拼接option,并给其中一个option赋值

1、jsp页面

   //拼接option,并根据前台传递的数据选中其中一个option
    $(function(){
          $.ajax({
                url: '/bulletin/bulletinAction!getStautsSelectOption.do',
                dataType: "json",
                type : "post",
                async : false,
                success: function (data) {  
                    var selectedValue = '${bulletinData.status}';
                    for (var i = 0; i < data.bulletinStatus.length; i++) {
                        var optionStr = "<option value='" + data.bulletinStatus[i].value +  "'";
                        if(data.bulletinStatus[i].value == selectedValue){
                            optionStr += " selected ";
                        }
                        optionStr = optionStr + ">" + data.bulletinStatus[i].name + "</option>";
                        $("#status").append(optionStr);
                    };
                }

              }); 


                document.getElementById("content").value = "${bulletinData.content}";
                var str = $('#content').val();
                ue.addListener("ready",function(){  
                ue.setContent(str);
              });
    }); $(function(){
          $.ajax({
                url: '/bulletin/bulletinAction!getStautsSelectOption.do',
                dataType: "json",
                type : "post",
                async : false,
                success: function (data) {  
                    var selectedValue = '${bulletinData.status}';
                    for (var i = 0; i < data.bulletinStatus.length; i++) {
                        var optionStr = "<option value='" + data.bulletinStatus[i].value +  "'";
                        if(data.bulletinStatus[i].value == selectedValue){
                            optionStr += " selected ";
                        }
                        optionStr = optionStr + ">" + data.bulletinStatus[i].name + "</option>";
                        $("#status").append(optionStr);
                    };
                }

              }); 


                document.getElementById("content").value = "${bulletinData.content}";
                var str = $('#content').val();
                ue.addListener("ready",function(){  
                ue.setContent(str);
              });
    });

2.前台代码

    //获取修改的公告状态
    public void getStautsSelectOption(){
        //同步来源select选项
        List<SysDataDictionary> statuss = Constants.dataDictionaryService.getDataDictionariesByDgroup("select", "公告状态");
        JSONArray bulletinStatus = new JSONArray();
        for(SysDataDictionary dic :statuss){
            JSONObject statusJSON = new JSONObject();
            statusJSON.put("name", dic.getDname());
            statusJSON.put("value", dic.getDvalue());
            bulletinStatus.add(statusJSON);
        }

        JSONObject echo = new JSONObject();
        echo.put("bulletinStatus", bulletinStatus);
        PrintWriter pw =null;
        try {
            pw = response.getWriter();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            pw.write(echo.toString());
            pw.flush();
            pw.close();
        }

    }

猜你喜欢

转载自blog.csdn.net/Ling1604/article/details/72674169