我试图用AJAX和Code Igniter从数据库中删除用户。当我点击删除链接时,用户被删除,但页面被重定向并显示成功消息。 AJAX似乎不能在我的代码中工作。 这里的HTML:使用AJAX和Codeigniter从数据库中删除用户
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Username</th>
<th>Password</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $u){?>
<tr>
<td><?php echo $u['id']; ?></td>
<td><?php echo $u['firstname']; ?></td>
<td><?php echo $u['lastname']; ?></td>
<td><?php echo $u['email']; ?></td>
<td><?php echo $u['username']; ?></td>
<td><?php echo $u['password']; ?></td>
<td>
<a href="#" >Edit</a> |
<?php $id=$u['id'];?>
<a href="<?php echo site_url("users/delete/$id")?>" class="delete">Delete</a>
</td>
<?php }?>
</tr>
</tbody>
</table>
和这里的AJAX:
$(document).ready(function(){
$(".delete").click(function(){
alert("Delete?");
var href = $(this).attr("href");
var btn = this;
$.ajax({
type: "GET",
url: href,
success: function(response) {
if (response == "Success")
{
$(btn).closest('tr').fadeOut("slow");
}
else
{
alert("Error");
}
}
});
})
});
,最后控制器功能删除用户笨
public function delete($id)//for deleting the user
{
$this->load->model('Users_m');
$delete=$this->Users_m->delete_user($id);
if($delete)
{
echo "Success";
}
else
{
echo "Error";
}
}
我在哪里做的错误?
你是正确的event.preventDefault()帮助。我试图添加alert(),说用户已被删除,但fadeOut()不起作用。这是为什么? – sabin 2013-04-21 09:58:28
嗯,fadeOut的代码看起来对我来说是正确的。我不确定表格行是否可以淡化。也许你可以用'.hide()'来代替它。 – Aeolun 2013-04-22 12:32:41