[英]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
?>
所以我想知道的是:
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
的引號。
我認為您通過將click事件綁定到一個ID上來犯了一個錯誤,因為您將有多行。 您需要共享HTML才能解決此問題。
一個瘋狂的猜測。 將#deleteRowButton(id)更改為.deleteRowButton(類)。
另請注意,attr()函數僅接受字符串作為參數。
$(this).closest('tr')。attr(“ id”);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.