我希望使用“编辑”和“删除”按钮,使用JSP删除一个HTML页面中的DB记录。我尝试了下面的代码。但只有第一个按钮正在工作(无论我放在哪个顶部),第二个按钮不起作用。任何人都可以帮忙..?
<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function edit(regno){
var f=document.form;
f.method="post";
f.action='edit.jsp?regno='+regno;
f.submit();
}
function delete(regno){
var f=document.form;
f.method="post";
f.action='delete.jsp?regno='+regno;
f.submit();
}
</script>
</head>
<body>
<form method="post" name="form">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Edit</th><th>Delete</th></tr>
<%
Connection conn = null;
int sumcount=0;
Statement st;
try{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:mydb1");
String query = "select * from student";
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
%>
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><input type="button" name="delete" value="Delete"
onclick="delete(<%=rs.getString(1)%>);"> </td>
<td><input type="button" name="edit" value="Edit"
onclick="edit(<%=rs.getString(1)%>);"> </td>
</tr>
<%
}
%>
<%
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>
答案 0 :(得分:0)
下面的示例工作正常,您可以在控制台中检查单击按钮后发送表单的位置。
<form method='post' id='form'>
<table>
<tr>
<td><input type='button' data-action='delete' data-regno='2' value='delete' /></td>
<td><input type='button' data-action='edit' data-regno='3' value='edit' /></td>
</tr>
</table>
</form>
var form = document.getElementById('form');
form.addEventListener('click', function(e) {
var type = e.target.getAttribute('data-action'),
regno = e.target.getAttribute('data-regno');
//console.log(type+'.jsp?regno='+regno);
form.action = type+'.jsp?regno='+regno;
form.submit();
}, false);
答案 1 :(得分:0)