如何在没有Ajax的情况下将jQuery中的隐藏值传递给Spring MVC控制器

时间:2015-09-30 08:22:47

标签: javascript jquery spring spring-mvc

我有下表

    <table id="table">
            <thead>
            <tr>
                <th></th>
               ...
            </tr>
            </thead>
            <tbody>
            <c:choose>
                <c:when test="${not empty userList}">
                    <c:forEach var="user" items="${userList}">
                        <tr>
                            <td><input name="id" value="${user.id}" hidden></td>
                            ...
                        </tr>
                    </c:forEach>
                </c:when>
            </c:choose>
            </tbody>
        </table>
<input type="button" name="objects"/>

这是我的javascript来选择表格中的行

$(window).load(function () {
    $("#table tr").click(function () {
        $(this).addClass('selected').siblings().removeClass('selected');
        var elem = $(this).find('td:first input').attr('value');
    });

    $('.objects').on('click', function (e) {
        alert($("#table tr.selected td:first input").attr('value'));
    });
});

当我选择行然后按下按钮objects时,我会收到所选ID的提醒。我想在Spring MVC控制器中传递这个id,在没有Ajax的情况下是可取的。

所以我希望在表格中选择用户,然后按下按钮objects并将所选用户的ID传递到新页面/userObjects。请帮帮我。

1 个答案:

答案 0 :(得分:1)

删除您的alert,然后执行以下操作:

var id = $("#table tr.selected td:first input").attr('value');
window.location = '/userObjects?id=' + id;

您可能需要调整window.location,具体取决于您要转发用户的网址。