我想通过复选框从数据库中删除多行我有“检查全部”的工作脚本但是当我想删除一两个时,什么也没发生。
JavaScript
<script type="text/javascript">
jQuery(function($) {
$("form input[id='check_all']").click(function() { // triggred check
var inputs = $("form input[type='checkbox']"); // get the checkbox
for(var i = 0; i < inputs.length; i++) { // count input tag in the form
var type = inputs[i].getAttribute("type"); // get the type attribute
if(type == "checkbox") {
if(this.checked) {
inputs[i].checked = true; // checked
} else {
inputs[i].checked = false; // unchecked
}
}
}
});
$("form input[id='submit']").click(function() { // triggred submit
var count_checked = $("[name='data[]']:checked").length; // count the checked
if(count_checked == 0) {
alert("Please select a comment(s) to delete.");
return false;
}
if(count_checked == 1) {
return confirm("Are you sure you want to delete these comment?");
} else {
return confirm("Are you sure you want to delete these comments?");
}
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$('.submit').click(function(){
var checkValues = $('input[name=data[]]:checked').map(function()
{
return $(this).val();
}).get();
$.ajax({
url: 'resources/ajax/ajax_delete_comment.php',
type: 'post',
data: { data: checkValues },
success:function(data){
}
});
});
});
</script>
HTML/PHP
<form method="post" id="form">
Check All <input type="checkbox" id="check_all" value="">
Here im displaying record from database and <input name=\"data[]\" type=\"checkbox\" id=\"data\" value=" . $row['id'] . ">
<input name="submit" class="submit" type="submit" value="Delete" id="submit">
</form>
删除脚本
if(isset($_POST['data'])) {
$id_array = $_POST['data']; // return array
$id_count = count($_POST['data']); // count array
for($i=0; $i < $id_count; $i++) {
$id = $id_array[$i];
$sql = $db->query("DELETE FROM comments WHERE `id` = '$id'");
if ($sql)
{
echo "success";
}
else
{
echo "Failed to delete the comment.";
}
}}
所以它的工作是检查所有,但是当我检查一两个对象时,什么也没发生,也许有人可以帮忙?