简体   繁体   English

如何在不使用Ajax的情况下将隐藏值从jQuery传递到Spring MVC控制器

[英]How to pass hidden value from jQuery to Spring MVC controller without Ajax

I have the following table 我有下表

    <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"/>

and here is my javascript to select row in my table 这是我的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'));
    });
});

When I select row and then press button objects I get an alert of selected id. 当我选择行然后按按钮objects我会收到有关所选ID的警报。 I'd like to pass this id in Spring MVC controller, desirable without Ajax. 我想在Spring MVC控制器中传递此id,最好在没有Ajax的情况下传递。

So I want select user in table, then press button objects and pass id of selected user to the new page /userObjects . 所以我想在表中选择用户,然后按按钮objects并将所选用户的ID传递到新页面/userObjects Can you help me with this, please. 请你帮我一下。

Remove your alert and just do something like : 删除alert然后执行以下操作:

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

You may have to adapt the window.location depending on the url on which you want to forward your user. 您可能必须根据要转发用户的URL调整window.location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM