繁体   English   中英

从会话数组Jquery Ajax中删除项目

[英]Removing item from session array Jquery Ajax

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

$(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(类)。

另请注意,attr()函数仅接受字符串作为参数。

$(this).closest('tr')。attr(“ id”);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM