以下是代码:
....
<table>
<?php
$count=1;
while ($row = $result->fetch_row()) {
?>
<tr>
<td>data 1</td><td>data 2</td><td><input type="checkbox" id="<?php $count; ?>"></td>
</tr>
<?php
$count++;
}
</table>
<input type="button" id="del" value="delete">
?>
....
对于我的js文件:
$("#del").click( function() {
$("table input").each(function(){
if($(this).prop("checked")) {
$product_key = $(this).attr('id');
$checked_row = $(this).closest('tr');
$.post("one.php", {product_id: $product_key}, function(){
$checked_row.remove();
});
}
});
});
如果勾选了多个复选框,则每次单击删除按钮时只删除一行。为什么?我想删除我勾选的所有行。怎么样?
答案 0 :(得分:0)
存储所有checked_rows,并在 .each
答案 1 :(得分:0)
var $product_key = $(this).attr('id');
var $checked_row = $(this).closest('tr');
你必须使用var
否则这些变量具有全局范围。
所以在$.post
的回调中,$checked_row
引用了最后一个。
答案 2 :(得分:0)
尝试
$("#del").click( function() {
$("table input:checked").each(function(){
var $product_key = $(this).attr('id');
var $checked_row = $(this).closest('tr');
$.post("one.php", {product_id: $product_key}, function(){
$checked_row.remove();
});
});
});