正在从会话数组Jquery Ajax中删除项


Removing item from session array Jquery Ajax

我有一个表,其中包含数组中每个元素的一行。我正试图在单击删除图像时删除该元素(图像的id为deleteRowButton)。目前,当您单击图像时,没有发生任何事情,但是,如果我删除var index行,表行将按预期淡出(当然,数组元素不会被删除。

$(document).on('click', '#deleteRowButton', function() {
    var index = $(this).closest('tr').attr(id); 
   //Set index to the id of table row (number corresponding to their position in the array)
    remove_index(index); 
    $(this).closest("tr").fadeOut('slow');
    return false;
});
function remove_index(value) {
    $.post("./remove_array_item.php", {index:value});
}       

以下是我在remove_array_item.php中删除数组元素的代码:

<?php  
include("./config/init.php"); //Contains session_start() etc. 
$index = $_POST['index']; //index variable passed in
$ca = $_SESSION["objColl"]; //get array from session variable
$ca->delLineItem($index); //delete item of array at index
$_SESSION["objColl"] = $ca; //store array as session variable 
?>

所以我想知道的是:

  1. 如果我包含以下内容,为什么jquery函数会停止激发:

var index = $(this).closest('tr').attr(id);

即,如果我将其注释掉,该函数将使表行淡出。

  1. 我在remove_array_item.php中的函数有什么问题,因为即使我删除var index并只传递索引的值

remove_index(1);

它仍然不会从数组中删除该项,因此当我刷新页面时,该项会回到表中。

var index = $(this).closest('tr').attr(id);更改为var index = $(this).closest('tr').attr('id');您忘记了id周围的引号。

我认为您犯了一个错误,将click事件绑定到一个Id,因为您将有多行。您需要共享HTML来澄清这个问题。

胡乱猜测。将#deleteRowButton(id)更改为.deleteRowButton(class)。

还要注意,attr()函数只接受字符串作为参数。

$(this).最接近('tr').attr("id");