使用Onclick将参数从一个jsp页面传递到另一个页面

To pass a parameter form one jsp page to another using Onclick

本文关键字:jsp 另一个 一个 参数 Onclick 使用      更新时间:2023-09-26

我需要将id参数从task.jsp传递到edit.jsp

我尝试使用输入类型为隐藏的set and get参数,但无法在edit.jsp 中获取id

这是我task.jsp页面的一部分

<tr>
                <%-- <td><%=currentTask.getId()%></td> --%>
                <td><%=currentTask.getTaskDescription()%></td>
                <td><%=currentTask.getDate()%></td>
                <td><%=currentTask.getUser().getUserName()%></td>
                <td><input id="<%=currentTask.getId()%>" type="button"
                    onclick="editAction(this)" value="Edit" /></td>
                <td><input id="<%=currentTask.getId()%>" type="button"
                    onclick="deleteAction(this)" value="Delete" /></td>
            </tr>
            <%
                }
            %>
        </table>
    </div>
    <p />
    <input type="button"
        onclick="window.location='/HibernateWebApp/login.jsp';"
        value="Logout">
    <%
        System.out.println(userName);
    %>
</form>
<b><a href="CreateTask.jsp?userName=${userName}"> Click here
        to add a new task </a></b>

<script>
    function editAction(item) {
        int id = item.id;
</script>
<input type="hidden" name="id" id="id" />
<script>
    document.getElementById("id").value = id;
        document.form.action = "edit.jsp";
        document.form.submit();
        console.log(item.id);
    }
</script>

<script>
    function deleteAction(item) {
        console.log(item.id);
    }
</script>

这是我点击编辑按钮ReferenceError: editAction is not defined 时在控制台中出现的错误

如何从task.jsp传递参数并获得edit.jsp

以任何一种方式做同样的事情。。。

task.jsp

<a href="t1.jsp?val=<%=currentTask.getId()%>">Edit</a>
    <!-- OR -->
    <form action="edit.jsp" method="post">
        <input id="id_anything123" type="hidden" name="val" value="<%=currentTask.getId()%>" />
        <input id="<%=currentTask.getId()%>" type="submit" value="Edit" />
    </form>

edit.jsp

 <%= request.getParameter("val") %> <!-- will print value which you have pass from task.jsp -->

我也遇到了这个问题,我找到了这样的方法:

在html:中

<button><a href="ServletName?id=${UserName}">ButtonName</a></button>

在servlet中:

request.getParameter("id");