2014-10-10 36 views
0

我有一个表中包含一个数组中的每个元素的行。我试图在点击删除图像时删除元素(图像的ID为deleteRowButton)。此刻,当你点击图片,但是如果我删除var index线,该表行将淡出预期(但当然数组元素不会删除没有任何反应。从会话数组中删除项Jquery Ajax

$(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);

即,如果我注释掉,该功能将使表行淡出。

  • 因为即使当我删除var index,只是传递一个值,该指数像这样
  • remove_index(1);

    什么是错我的remove_array_item.php功能它仍然不会从数组中删除项目,所以当我刷新页面时,该项目回到表格中。

    回答

    1

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

    1

    我想你犯了一个错误,将点击事件绑定到一个Id,因为你将有多行。 你需要分享HTML来澄清这个问题。

    疯狂的猜测。将#deleteRowButton(id)更改为.deleteRowButton(class)。

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

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