当我的网页发生更改时,我尝试填充两个单独的 HTML 对象 - 一个是下拉列表,另一个是表单。
所以在我的 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
似乎只允许我在每次更改时填充一个对象。
最佳答案
就您而言,发送两个 ajax 请求并使用两个 spring Controller 来处理每个请求并不是一个坏主意。随后,在 JSP 页面中,您将需要两个 javascript 回调来填充不同的 HTML 部分。
或者,如果您坚持只发出一个 AJAX 请求;解决方法是让 spring Controller 返回一个 JSON,其中包含用于填充这两个 HTML 部分的数据。但使用这种方法显然需要更多的 JavaScript 工作。
关于java - 通过 Java 从 Javascript 更改填充多个 HTML 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15243998/