2011-07-27 196 views
-1

出于某种原因,它只会删除第一行,不知道为什么。不删除其他行

function templatesArray(whatsThis) { 
    var myNewArray = new Array(); 
    var aRow = new Array(); 

    $('input:checkbox[name="templates"]:checked').each(function(i) { 
     myNewArray.push($(this).val()); 
     aRow.push(oTable.fnGetPosition($(this).parents('tr').get(0))); 
    }); 
    var dataString = 'templatesArray=' + myNewArray + '&deleteTemplatesArray=True'; 
    $.ajax({ 
     type: "POST", 
     url: "processes/templates.php", 
     data: dataString, 
     success: function(data) { 
      if (data.errorsExist) { 
      } else { 
       $(whatsThis).parents("tr").eq(0).hide(); 
       for (i in aRow) // loop over the array of row indexes 
        oTable.fnDeleteRow(aRow[i]); 
       if(oTable.fnSettings().fnRecordsTotal() == 0) { 
        $('.bt_red').remove(); 
        $('.bt_blue').remove(); 
       } 
       if(oTable.fnSettings().fnRecordsTotal() <= 10) { 
        $('.bt_blue').remove(); 
       } 
       if(oTable.fnSettings().fnRecordsTotal() <= 10) { 
        $('div.pagination').remove(); 
       } 
      } 
     } 
    }); 

} 
+1

什么是oTable?标记在哪里?你需要比1班轮和一些代码更加冗长。 – madcapnmckay

+0

http://jsfiddle.net/xtremer360/3Ldj5/它应该执行它所做的服务器端操作,然后从表中删除该行,但仅执行数组中的第一个操作。 –

+0

为什么我的文章被编辑? –

回答

2

避免。你可以得到意想不到的结果而是使用Array.foreach或jQuery的$.each()

$.each(aRow, function() { 
    oTable.fnDeleteRow(this); 
}); 

如果仍不能解决问题,请发布更多细节,如oTable.fnDeleteRow()代码。是否有任何JavaScript错误被抛出?

编辑:您的代码正在将行索引传递给删除函数。在调用delete函数之后,所有正在进行的行都有一个新的行索引。所以,如果你试图删除行1,2,3,你最终会删除行1,3和5相反存储和传递给该行本身的引用:

$('input:checkbox[name="templates"]:checked').each(function(i) { 
    myNewArray.push($(this).val()); 
    aRow.push($(this).closest('tr')[0]); 
}); 

http://jsfiddle.net/3Ldj5/2/

+0

我已经在jsfiddle中包含了它的副本。 http://jsfiddle.net/xtremer360/3Ldj5/ –

+0

@jeff - 我已经更新了我的答案。 – gilly3

1

我认为你是选择AROW指数incorreclt只是一个猜测 如果这样做的目的(在你的“选中”循环)

$(this).parents('tr').get(0) 

是选择行尝试:

$(this).closest('tr') 

,并在阿贾克斯成功回调,而不是

$(whatsThis).parents("tr").eq(0).hide(); 

尝试使用数组for .. in

$(whatsThis).closest("tr").hide();