1. 程式人生 > >拼接option,並給其中一個option賦值

拼接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();
        }

    }