2013-11-25 147 views
0

我想從我的MS訪問數據庫中使用複選框在我的HTML/JSP文件中刪除一行。該文件當前將數據庫輸出到用戶界面,勾選複選框並按下刪除後,會彈出一個提示框。但我仍然不能刪除我的數據庫中的行。任何幫助將非常感激。謝謝。Html-從數據庫中刪除一行

見下面的代碼:

<%@ 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> 
+0

很高興再次見到你,但這是我真正期待的代碼。 –

+0

嗨,再次,認爲一個新的職位將是最好的方法來幫助解決這個問題。 –

+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> 

忘掉阿賈克斯最初,通過使用console.log(empIds)alert(empIds);確保您將獲得按鈕 點擊正確的行號在你的功能裏面。希望你一定會得到這個時間,如果你的數據庫部分是正確的。

+0

嗨Vinoth,再次感謝您的更新。對不起,但它不工作。當我使用上面的代碼時,不會彈出刪除確認提示。 –

+0

如果我放入下面的代碼,它會從我的數據庫中刪除一條記錄,但問題是我必須在代碼中設置該標識,因此在這種情況下它的值爲103。 DELETE FROM Employees WHERE Id =? ' –

+1

不是將您的請求傳遞給jsp,而是將它傳遞給某個servlet類。在那裏你可以收到該ID並做你的刪除。 JSP不是推薦數據庫操作的地方。 –