我有一个表,其中包含数组中每个元素的一行。我试图在单击删除图像时删除该元素(图像的 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
?>
所以我想知道的是:
- 如果我包含以下内容,为什么 jquery 函数会停止触发:
var index = $(this).closest('tr').attr(id);
即如果我将其注释掉,该函数将使表格行淡出。
- 我在
remove_array_item.php
中的函数有什么问题,因为即使我删除var index
并只像这样传递索引值
remove_index(1);
它仍然没有从数组中删除该项目,因此当我刷新页面时,该项目又回到了表中。
最佳答案
将 var index = $(this).closest('tr').attr(id);
更改为 var index = $(this).closest('tr')。 attr('id');
您忘记了 id
周围的引号。
关于javascript - 从 session 数组中删除项目 Jquery Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26299607/