3

这是我的代码的一部分,我正在使用 ajax 发布和删除数据,一切正常,除了数据没有从我的数据库中删除。我究竟做错了什么?开始了:

文件视图.php

<table class='uk-table uk-table-striped'><tbody>
<thead>
<tr>
<th>Ano</th>
<th>Grau</th>
<th>Serie</th>
<th>Curso</th>
<th>Instituição</th>
<th>Cidade</th>
<th>Estado</th>
<th>Excluir?</th>
</tr>
</thead>

<script type="text/javascript">
$(document).ready(function() {
$('#load').hide();
});

$(function() {
$(".delete").click(function() {


 if (confirm("Tem certeza?"))
                {
                    var row = $(this).parents('tr:first');
                    var id = $(this).attr("id");
                    var data = 'id=' + id ;

$.ajax({
   type: "post",
   url: "delete.php",
    data: data,
   cache: false,
   success: function(){ 
    row.slideUp('slow', function() {$(row).remove();});

  }


 });


 }

return false;
    });
});


echo "<tr id='".$historico['id']."'>";
echo "<td>".$historico['ano']."</td>";
echo "<td>".$grau['grau']."</td>";
echo "<td>".$serie['serie']."</td>";
echo "<td>".$curso['curso']."</td>";
echo "<td><a href='index.php?option=com_community&view=groups&task=viewgroup&groupid=".$grupo['id']."'>".$grupo['name']."</a></td>";
echo "<td>".$cidade['nome']."</td>";
echo "<td>".$estado['sigla']."</td>";
echo "<td><button class='delete uk-button uk-button-danger'>Delete</button></td>";
echo "</tr>";</tbody></table>

文件删除.php

<?php 

include 'con.php';
$id= $_POST["id"];

$query=mysql_query("DELETE FROM historico WHERE id = '$id'")or die(mysql_error());




?>
4

2 回答 2

2
var id = $(this).attr("id");

这是试图读取被点击按钮的 id。您需要 tr 的 id:

var id = row.attr("id");

你也不需要:first,你只需要tr包含按钮的:

var row = $(this).parents('tr');

parent('tr')应该工作,而不是parents. (父母可能工作,但没有必要。)

于 2013-08-11T02:50:58.283 回答
0

Make things more simpler print the id in each delete button.

echo "<td><button class='delete uk-button uk-button-danger' id='".$historico['id']."'>Delete</button></td>";

And then use your code as it is.

As Andy G said You have to remove or modify the id attribute value of your tr

于 2013-08-11T03:13:52.373 回答