Html-从数据库中删除一行

时间:2013-11-25 17:04:01

标签: javascript html database jsp checkbox

我正在尝试使用HTML / JSP文件中的复选框从MS访问数据库中删除一行。该文件当前将数据库输出到UI,勾选复选框并按下删除后,会弹出一个提示。但我仍然无法删除数据库中的行。任何帮助将非常感激。谢谢。

请参阅以下代码:

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<html>
<head>
<title>DELETE Operation</title>
<script type="text/javascript">
function deleteEmployee(rowid){
input_box = confirm("Are you sure you want to delete this Record?");
if (input_box == true) {
    // Output when OK is clicked
        $('#' + rowid).remove();
        alert('Record Deleted');
} else {
    // Output when Cancel is clicked
    return false;
}
  }
</script>
</head>
<body>

<sql:setDataSource
    var = "bookdB"
    scope = "session"
    driver = "sun.jdbc.odbc.JdbcOdbcDriver"
    url = "jdbc:odbc:bookdB"
/>

<sql:query dataSource="${bookdB}" var="result">
   SELECT * from Employees;
</sql:query>

<table border="1" width="100%">
<tr>
   <th></th>
   <th>Emp ID</th>
   <th>First Name</th>
   <th>Last Name</th>
   <th>Age</th>
</tr>

<c:forEach var="row" items="${result.rows}">
<form method="post" action="reserve.jsp">
<tr id="${row.id}">
    <td><input type = "checkbox" value="${row.id}" name="empIds"></td>
    <td><c:out value="${row.id}"/></td>
    <td><c:out value="${row.first}"/></td>
    <td><c:out value="${row.last}"/></td>
    <td><c:out value="${row.age}"/></td>
</tr>
</form>
</c:forEach>
<input type="button" value="delete" onclick="deleteEmployee('${row.id}');" />
</table>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

很好,这可以帮助您解决问题。看看下面的脚本,

<script type="text/javascript">
function deleteEmployee(){

var empIds = [];
$("input[name='empIds']").each(function() {
empIds.push($(this).val());
});

console.log(empIds);

input_box = confirm("Are you sure you want to delete this Record?");
if (input_box == true) {
// Output when OK is clicked

 // Uncomment this code for check your UI part.
 //  $("input[name='empIds']").each(function() {
 //       $('#' + $(this).val()).remove();
 //   });
 //   alert('Record(s) Deleted');

$.ajax({
    url : 'reserve.jsp?empIds='+empIds,
    type : "POST",
    async : false,
    success : function() {
        $("input[name='empIds']").each(function() {
            $('#' + $(this).val()).remove();
        });
        alert('Record(s) Deleted');
    }
});

    $('#' + rowid).remove();
    alert('Record Deleted');
} else {
// Output when Cancel is clicked
return false;
}
}
</script>

在你的HTML部分,

<table border="1" width="100%">
<tr>
  <th></th>
  <th>Emp ID</th>
  <th>First Name</th>
  <th>Last Name</th>
  <th>Age</th>
</tr>

<c:forEach var="row" items="${result.rows}">
<form method="post" action="reserve.jsp">
<tr id="${row.id}">
<td><input type = "checkbox" value="${row.id}" name="empIds"></td>
<td><c:out value="${row.id}"/></td>
<td><c:out value="${row.first}"/></td>
<td><c:out value="${row.last}"/></td>
<td><c:out value="${row.age}"/></td>
</tr>
</form>
</c:forEach>
<input type="button" value="delete" onclick="deleteEmployee();" />
</table>

最初忘掉ajax,确保你在按钮上获得正确的行ID 点击功能中的console.log(empIds)alert(empIds);。如果您的数据库部分正确,希望您肯定会得到这个时间。