2013-03-06 26 views
0

當我的網頁發生更改時,我試圖填充兩個單獨的HTML對象 - 一個下拉列表和一個表單。通過Java填充多個HTML語句從Javascript更改

所以在我的JSP代碼我有這樣的:

$('#uSystemList').change(function() { 
    $.get("/live-application/systemById", {systemid: $("#uSystemList").val()}, displayChangedSystemResults, "html"); 
}); 

<script type="text/javascript"> 
function displayChangedSystemResults(html){ 
    $('#uList').empty(); 
    $('#uList').append(html); 
} 
</script> 

而且在Java端我有這樣的:

@RequestMapping(value = "/systemById", method = RequestMethod.GET) 
public void getSystemById(@RequestParam("systemid") String systemid, OutputStream outputStream) throws IOException { 
    StringBuilder refreshHtml = new StringBuilder(); 
    try { 
     String html = ""; 
     System system = service.getSystemById(systemid)); 
     for (Value value: inewsSystem.getValues()) { 
      html = html + "<option value='" + value.getId() + "'> " + value.getName() + "</>"; 
      } 
     } 
     refreshHtml.append(html); 
     outputStream.write(refreshHtml.toString().getBytes()); 
     outputStream.flush(); 
    } catch (Exception e) { 
     outputStream.flush(); 
    } 
} 

使填充我的uList下拉 - 但我怎麼告訴它填充其他內容(例如表單,但可以是其他任何事物作爲示例...)。 OutputStream似乎只允許我爲每個更改填充一個對象。

回答

1

在你的情況,這不是一個壞主意,派兩名Ajax請求,並有處理每個請求兩個彈簧控制器。隨後,在JSP頁面中,您需要兩個JavaScript回調來填充不同的HTML部分。

或者,如果您堅持只提出一個AJAX請求,一個解決方法就是讓Spring控制器返回一個JSON,其中包含用於填充這兩個HTML部分的數據。但採用這種方法顯然需要更多的JavaScript努力。

0

這是Javascript不是JSP :)。您需要更新:

<script type="text/javascript"> 
function displayChangedSystemResults(html){ 
    $('#uList').empty(); 
    $('#uList').append(html); 
    $('#someOtherElement').empty(); 
    $('#someOtherElement').append(html); 
} 
</script> 
+0

對不起,我的意思是從一個JSP頁面。好的,謝謝你的帖子。但它如何知道html的哪個部分是用於ulist的,哪些是用於某些元素的? – maloney 2013-03-06 10:01:33