【问题标题】:Create a dynamic html in Jquery from session variable从会话变量在 Jquery 中创建动态 html
【发布时间】: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 部分经过测试且无错误。
  • 在下面查看我的答案。希望对您有所帮助。

标签: java jquery ajax


【解决方案1】:
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>';
    }

试试这个代码。

【讨论】:

  • 感谢尝试 Smit,但它仍然抛出同样的错误。
【解决方案2】:

仍然需要一些建议来通过 JSP 迭代我的数组: 当我直接调用 ${applicationScope.tableStatus[0][0]} 时,它返回正常,但无法使用 i 和 j 进行迭代。

function printGame(){   
var html = "";
html+='<table>';
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+'">' + '${applicationScope.tableStatus[0][0]}' + '</td>';
        }
    // add closing </tr> tag to the string:
    html += '</tr>';
}
html+='</table>';
return html;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多