簡體   English   中英

無法顯示表行 jquery

[英]Unable to show table row jquery

我是兩個表,例如表 A 和表 B。當我選中表 A 上的復選框時,我可以附加到表 B。我面臨的問題是在從表 B 中刪除行時。當我反向刪除行時訂購一切正常。 意思是從下往上刪除。 行在表 A 中恢復。但如果我以隨機順序刪除,例如,我有三行,當我刪除第一行時,它會恢復到表 A,但第二行和第三行不會。 如果我刪除第二行,然后刪除第一行。 他們得到恢復,但第三行沒有。

附上截圖:

表 A截圖 1

表 B截圖 2

這是代碼:

$(".delete-row").click(function(){
$('[name="sl"]').val('');
$(".hssl").empty();
  table A --   $(".invt-do").find('input[name="ssl[]"]').each(function(index){
  if($(this).is(":not(:checked)")){
       table B --- $(".sltable tbody tr").eq(index).each(function(){
         $(this).show();
         $(this).find('input[name="ssl[]"]').prop("checked", false);
       });
       $(this).parents("tr").remove();
       $(".hssl").append($(this).parents("tr")); 
       $(".hssl").find('input[name="ssl[]"]').prop("checked", true);
       // $('.sltable tbody tr').eq(index).show();
       //$(".sltable tbody").find('input[name="ssl[]"]').prop("checked", false);
    }
});
$.ajax({
  url:"<?php echo base_url(); ?>updateinvtunstock", //$("#mainsec").attr("action"),
  type:"POST",
  data:$("#dlvssl").serialize(),//only input
  success: function(response){
    $('#dlvdetails').modal('show');
  },
  error: function() {
    swal("Oops", "We couldn't connect to the server!", "error");
  }
}); 
});

不知道我做錯了什么。

首先在你的代碼index將返回輸入的索引我認為它每次都會返回 0 所以只有第一行會隱藏.. 不是行的索引

好的,讓我們從刪除按鈕的點擊事件開始

單擊刪除按鈕時

  • 循環檢查已檢查的輸入
  • 獲取最近的行索引然后刪除該行
  • 使用上面的行索引,您可以獲得具有相同索引的另一個表中的行。

看下一個代碼

$(".delete-row").click(function(){
  $(".invt-do").find('input[name="ssl[]"]').each(function(){  //loop through table a inputs
    if(this.checked){  // check if its checked
       var Row_Index = $(this).closest('tr').index();  // get checked input row index
       $(".sltable tbody tr").show().eq(index).hide();  // show all rows then hide/remove the same index from table b
       $(this).closest("tr").remove(); // then remove the row from table a
    }
  });
});

如果您仍然需要使用自己的方式進行編碼,則下一個代碼沒有意義

$(".sltable tbody tr").eq(index).each(function(){
       $(this).show();
       $(this).find('input[name="ssl[]"]').prop("checked", false);
}); 

你可以用下一個代碼替換它而不用循環

var Row_Index = $(this).closest('tr').index();
$(".sltable tbody tr").eq(Row_Index).show().find('input[name="ssl[]"]').prop("checked", false);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM