简体   繁体   English

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

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

I need to pass the id parameter from task.jsp to edit.jsp .我需要将 id 参数从task.jspedit.jsp

I tried using the set and get parameter with input type as hidden, but was unable to get the id in the edit.jsp我尝试使用 set 和 get 参数,输入类型为隐藏,但无法在 edit.jsp 中获取 id

This is a part of my task.jsp page这是我的 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>

This is the error I get in my console when I click on the edit button - ReferenceError: editAction is not defined这是我单击编辑按钮时在控制台中得到的错误 - ReferenceError: editAction is not defined

How do I pass the parameter from task.jsp and get it is edit.jsp如何从 task.jsp 传递参数并获取它是 edit.jsp

do something likewise either way... 无论哪种方式都做同样的事情...

task.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 edit.jsp

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

I had this problem too and I found this way to do :我也有这个问题,我找到了这样做的方法:

In the html :在 html 中:

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

In the servlet :在 servlet 中:

request.getParameter("id");

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

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