好吧,您可以通过以下几种方式做到这一点:
1) 使所有元素具有相同的形式。将每个复选框命名为相同,但为每个复选框指定一个值,以区分它所代表的记录/ID/文件。当浏览器(如果兼容)提交表单时,CGI 应用程序应该能够将 HTTP 参数视为 POST 或 GET 提交的一部分。许多像 PHP 这样的 CGI 应用程序将同名参数组合到一个数组中。您也可以随时使用 C 自己遍历参数列表。
// Client side html
<table>
<form>
<tr><td><input type="checkbox" name="id" value="1"/></td><td>Row 1</td></tr>
<tr><td><input type="checkbox" name="id" value="2"/></td><td>Row 2</td></tr>
<tr><td><input type="checkbox" name="id" value="3"/></td><td>Row 3</td></tr>
<tr><td><input type="checkbox" name="id" value="4"/></td><td>Row 4</td></tr>
</form>
</table>
// Server side CGI, using pseudo-code
String[] ids = request.getArrayOfParametersNamed("id");
if(!empty(ids)) {
for(id in ids) {
DatabaseControllerModelThingWhatever.deleteById(id);
}
// Actually if SQL based you should use a batch statement instead of
// one-at-a-time deletes like above
}
// Ok the rows are deleted, either print out the page, or better yet,
// send a redirect so that a user-refresh does not try and re-delete
// already deleted stuff and also give the user a wierd "resubmit form" warning
// Done
2) 使用 AJAX,最好是某种类型的 Javascript 库,当用户单击删除时,执行基于 ajax 的提交,提交删除检查记录的请求。同时使用 Javascript 从 HTML 表中删除行。这意味着用户的页面永远不会完全刷新,嗯,有点。
// Client side HTML is same as before, only this time there is a DELETE button with
// an onclick handler. Also, add a "class" or "id" to each "tr" so we can find it
// in the HTML table
// Pseudo-javascript because I am lazy
function onDeleteButtonClick() {
// Get our ids
var idElements = document.getElementsById("id");
// Submit an async AJAX request (e.g. use Jquery and send ids as URL params)
ajaxedDeleteSubmission(idElements);
// Delete all the rows that should not be there
for(i = 0; i < tablex.rows.length; i++) {
// Grab the value of the "id" attribute of each table row (<tr id="?">...</tr>)
id = tablex.rows[id].id;
if(id in ids) {
// Remove the row, forget how because now I just use Jquery.
tablex.deleteRow(i);
}
}
}