简体   繁体   English

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

[英]Removing item from session array Jquery Ajax

I have a table containing a row for each element in an array. 我有一个表,其中包含数组中每个元素的一行。 I am trying to remove the element when the delete image is clicked (the image's id is deleteRowButton ). 我试图在单击删除图像时删除该元素(图像的ID为deleteRowButton )。 At the moment nothing happens when you click the image, however if I remove the var index line, the table row will fade out as expected (but of course the array element is not removed. 目前,单击图像没有任何反应,但是,如果删除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});
}       

Here is my code in remove_array_item.php to remove the array element: 这是我在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 
?>

So what I am wondering is: 所以我想知道的是:

  1. Why does the jquery function stop firing if I include: 如果包括以下内容,为什么jquery函数停止触发:

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

ie if I comment it out, the function will make the table row fade out. 即,如果我将其注释掉,该函数将使表行淡出。

  1. What is wrong with my function in remove_array_item.php because even when I remove the var index and just pass a value for the index like so 我在remove_array_item.php函数出了什么问题,因为即使我删除了var index并且只是像这样传递索引值

remove_index(1);

It still does not remove the item from the array and so when I refresh the page, the item is back in the table. 它仍然不会从数组中删除该项目,因此当我刷新页面时,该项目又回到了表中。

Change var index = $(this).closest('tr').attr(id); 更改var index = $(this).closest('tr').attr(id); to var index = $(this).closest('tr').attr('id'); var index = $(this).closest('tr').attr('id'); You forgot the quotes around id . 您忘记了id的引号。

I think you made a mistake by binding on click event to an Id, where as you will have multiple rows. 我认为您通过将click事件绑定到一个ID上来犯了一个错误,因为您将有多行。 You need to share the HTML to clarify this issue. 您需要共享HTML才能解决此问题。

A wild guess. 一个疯狂的猜测。 Change the #deleteRowButton (id) to .deleteRowButton (class). 将#deleteRowButton(id)更改为.deleteRowButton(类)。

Also note that attr() function only accept string as parameter. 另请注意,attr()函数仅接受字符串作为参数。

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

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

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