0

我有一个脚本,它运行 AJAX 调用以从数据表中删除指定的行。查询运行良好,记录已从数据库中删除,但当用户单击删除按钮隐藏时,我无法获取用户正在浏览的表中的行。我想我记得jQuery有问题,td/tr也许就是这样,但即使在尝试将每个表格行放在自己的 div 中以引用我之后,我仍然没有运气。

ajax 请求:

$(document).ready(function() {
    //##### Send delete Ajax request to response.php #########
    //$("body").on("click", "#responds .del_button", function(e) {
        $("body").on("click", ".del_button", function(e) {
         e.returnValue = false;
         var clickedID = this.id.split('-'); //Split string (Split works as PHP explode)
         var DbNumberID = clickedID[1]; //and get number from array
         var myData = 'recordToDelete='+ DbNumberID; //build a post data structure

            jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "ajax/education_response.php", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:myData, //Form variables
            success:function(response){
                //on success, hide  element user wants to delete.
                $('#item_'+DbNumberID).fadeOut("slow");
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            }
            });
    });
    $('html, body').stop();

});

以及我正在使用的填充表格的结构(我知道divs每行都乱七八糟,但正如我所说,我确定我记得jQuery不喜欢tr/td。我在其他页面上使用了这个 ajax 调用,并且fadeout()在所有其他情况下都可以正常工作,所以肯定是因为我试图从前端表中删除单个行。

<?php
while ($row = mysqli_fetch_assoc($result)) {
    $catid = $row['id'];
    $catsector = $row['job_sector'];
    $cattype = $row['job_type'];
    $catname = $row['job_name'];
    echo "<div id=item_".$catid.">";
    echo "<tr class=''>";
    echo "<td>$catsector</td>";
    echo "<td>$cattype</td>";
    echo "<td>$catname</td>";
    echo "<td><a href='#' class='del_button' id='del-". $catid ."'>Delete CV</a></td>";
    echo "</tr>";           
    echo "</div>";
}
?>
4

2 回答 2

1

消除

echo "<div id=item_".$catid.">";

echo "</div>";

和改变

echo "<tr class=''>";

为了

echo "<tr class='' id=item_".$catid.">";
于 2013-06-26T20:17:50.823 回答
0

改变

var myData = 'recordToDelete='+ DbNumberID; //build a post data structure

        jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "ajax/education_response.php", //Where to make Ajax calls
        dataType:"text", // Data type, HTML, json etc.
        data:myData, //Form variables
        success:function(response){
            //on success, hide  element user wants to delete.
            $('#item_'+DbNumberID).fadeOut("slow");
        },
        error:function (xhr, ajaxOptions, thrownError){
            //On error, we alert user
            alert(thrownError);
        }
     });

为了

  var that = this;
  var myData = 'recordToDelete='+ DbNumberID; //build a post data structure

        jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "ajax/education_response.php", //Where to make Ajax calls
        dataType:"text", // Data type, HTML, json etc.
        data:myData, //Form variables
        success:function(response){
            //on success, hide  element user wants to delete.
                 $(that).closest('tr').fadeOut("slow"); // will works faster 
        },
        error:function (xhr, ajaxOptions, thrownError){
            //On error, we alert user
            alert(thrownError);
        }
  });

根据 Aguardientico 的回答更改标记也很有用。

于 2013-06-26T20:21:12.993 回答