如何使用jsp变量传输onclick并直接到其他页面

How to transfer onclick with a jsp variable and direct to other page

本文关键字:其他 jsp 何使用 变量 传输 onclick      更新时间:2023-09-26

有人知道如何将变量从按钮转移到其他页面吗?我正在创建一个更新和删除网站。数据库查询所有的数据,并有一个编辑按钮,如果我按编辑,它将直接到第二个站点。本网站将显示您需要更新的数据信息。

源代码如下。我使用下面的源代码,当我进入编辑,将不会直接到页面(edit_delete.jsp)谁能告诉我,代码有什么问题吗请查看我标记的源代码中的粗体,这可能是一个问题。

----------------------------------------- 源代码 -----------------------------------------------------------

<script language="javascript">
function editRecord(no){
var f=document.form1;
  alert(no);  
f.method="post";
**f.action='edit_delete.jsp?id='+no;**
f.submit();
}
</script>
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp?user=root&useUnicode=true&characterEncoding=big5");
     stmt=con.prepareStatement("select id, no, name, class, from test");
     rs = stmt.executeQuery();
   while(rs.next()) {
     int test = rs.getInt("no");
      out.print("<tr><td>");
      out.print(rs.getString("id"));
      out.print("</td><td>");
      out.print(rs.getString("no"));
      out.print("</td><td>");
      out.print(rs.getString("name"));
      out.print("</td><td>");
      out.print(rs.getString("class"));
     out.print("</td><td>");
     out.print("</td><td>");
      **out.println("<input type='button' name='edit' value='Edit' onclick='editRecord('rs.getInt('no'))'>");**
      out.print("</td></tr>");

    }
    %>

始终尽量避免使用 script ,而是使用更容易使用且更不容易出错的JavaServer Pages标准标记库和表达式语言。

SQL标签库是为快速原型和简单应用程序访问数据库而设计的。

示例代码:

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
...
<sql:setDataSource var="webappDataSource" driver="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/webapp?user=root&useUnicode=true&characterEncoding=big5"
    user="root" password="" />
<sql:query dataSource="${webappDataSource}" 
            sql="select id, no, name, class, from test" var="result" />
<table width="100%" border="1">
    <c:forEach var="row" items="${result.rows}">
        <tr>
            <td>${row.no}</td>
            <td>${row.id}</td>
            <td>${row.no}</td>
            <td>${row.name}</td>
            <td>${row.class}</td>
            <td><input type='button' name='edit' value='Edit' onclick='editRecord(${row.no})' /></td>
        </tr>
    </c:forEach>
</table>

你可以尝试使用Window open()方法代替表单提交,因为它是简单的get请求和值在URL中作为查询字符串传递。

例如

<input type='button' name='edit' value='Edit' onclick='window.open("edit_delete.jsp?id=${row.no}");'/>