【发布时间】:2018-03-24 07:26:10
【问题描述】:
我想使用 Jquery 为单独的 http 会话创建动态 html 内容。非常感谢您的支持。
//This is where I assign my array to a session attribute.
request.getSession().setAttribute("updateTable", game.getTableStatus());
我有这样的功能来创建具有从数组传递的值的 3X3 动态表。我在突出显示的行中使用的方式似乎是错误的。我可以根据你的建议来解决这个问题。
function writeTable() {
// declare html variable (a string holder):
var html = '';
for (var i = 0; i < 3; i++) {
// add opening <tr> tag to the string:
html += '<tr id="'i'">';
for (var j = 0; j < 3; j++) {
// add <td> elements to the string:
html += '<td id="'j'">' + **${sessionScope.updateTable[i][j]}** + '</td>';
count++;
}
// add closing </tr> tag to the string:
html += '</tr>';
}
//append created html to the table body:
$('#body').append(html);
// reset the count:
count = 0;
}
最后,是否可以将此表与 AJAX 集成,以便通过 onClick() 事件发回元素 id?
当它是一个静态表来满足 AJAX 要求时,我正在使用下面的代码。
$(document).ready(function(){
$("td").click(function(){
//$(this).attr('id');
$.ajax({
type: 'POST',
url: 'gameServlet',
data: { cellInfo: $(this).attr("id"), rowInfo: $(this).closest("tr").attr("id")},
success:
function(data, textStatus, jqXHR) {
//$(this).text("${sessionScope.mySign}");
},
error:
function(jqXHR, textStatus, errorThrown) {
$("#result").text(textStatus + ": Error");
},
dataType: 'text'
});
$(this).text("${sessionScope.mySign}");
});
});
非常感谢!
【问题讨论】:
-
您遇到了什么错误。我可以在您的代码中看到一些语法错误。
-
这是我在脚本中添加 ${sessionScope.updateTable[i][j]} 时得到的:/WEB-INF/views/gamePanel.jsp(行:95,列:5 ) #{...} 在模板文本中是不允许的。 AJAX 部分经过测试且无错误。
-
在下面查看我的答案。希望对您有所帮助。