2015-09-21 76 views
2

我對此代碼有問題。即使我有兩個或多個部分,我也無法通過單擊「刪除按鈕」刪除某個部分。重複部分在刪除時失敗

任何人都可以看到錯誤是什麼?

http://jsfiddle.net/4vs93yq3/

// Add a new repeating section 
var attrs = ['for', 'id', 'name']; 
function resetAttributeNames(section) { 
    var tags = section.find('input, label'), idx = section.index(); 
    tags.each(function() { 
     var $this = $(this); 
     $.each(attrs, function(i, attr) { 
     var attr_val = $this.attr(attr); 
     if (attr_val) { 
      $this.attr(attr, attr_val.replace(/_\d+$/, '_'+(idx + 1))) 
     } 
     }) 
    }) 
} 

$('.addFight').click(function(e){ 
     e.preventDefault(); 
     var lastRepeatingGroup = $('.repeatingSection').last(); 
     var cloned = lastRepeatingGroup.clone(true) 
     cloned.insertAfter(lastRepeatingGroup); 
     resetAttributeNames(cloned) 
    }); 

// Delete a repeating section 
$('.deleteFight').click(function(e){ 
     e.preventDefault(); 
     var current_fight = $(this).parent('div'); 
     var other_fights = current_fight.siblings('.repeatingSection'); 
     if (other_fights.length === 0) { 
      alert("You should atleast have one item"); 
      return; 
     } 
     current_fight.slideUp('slow', function() { 
      current_fight.remove(); 

      // reset fight indexes 
      other_fights.each(function() { 
       resetAttributeNames($(this)); 
      }) 

     }) 


    }); 

這裏是我的HTML

<div class="repeatingSection"> 
         <section class="panel"> 
          <header class="panel-heading"> 
          Item information 
          <div class="pull-right"><a href="#" class="btn btn-danger btn-sm deleteFight"><i class="fa fa-trash-o"></i>Remove</a></div> 
          </header> 
          <div class="panel-body"> 
           <div class="form">          
            <div class="form-group "> 
             <label for="itemnumber_1" class="control-label col-lg-2">Itemnumber</label> 
             <div class="col-lg-10"> 
              <input class=" form-control" id="itemnumber_1" name="itemnumber_1" type="text" /> 
             </div> 
            </div> 
            <div class="form-group "> 
             <label for="quantity_1" class="control-label col-lg-2">Quantity</label> 
             <div class="col-lg-10"> 
              <input class=" form-control" id="quantity_1" name="quantity_1" type="text" /> 
             </div> 
            </div> 
            <div class="form-group "> 
             <label for="reason_1" class="control-label col-lg-2">Reason for return</label> 
             <div class="col-lg-10"> 
              <textarea class=" form-control" id="reason_1" name="reason_1"></textarea> 
             </div> 
            </div> 

           </div> 
          </div> 
         </section> 
         </div> 

         <section class="panel"> 

          <div class="panel-body"> 
           <div class="form">          
             <div class="form-group"> 
              <div class="col-lg-offset-2 col-lg-10"> 
               <a href="#" class="btn btn-info addFight"><i class="fa fa-plus"></i> One more item</a> 
              </div> 
             </div> 
           </div> 
          </div> 
         </section> 
+0

您可以請創建jsfiddle – Mitul

回答

1

在這裏你去。檢查更詳細的解釋內嵌的意見和檢查DEMO here

// Delete a repeating section 
$('.deleteFight').click(function(e){ 
     e.preventDefault(); 
     var current_fight = $(this).parent('div'); 
     var other_fights = current_fight.closest('.repeatingSection').siblings(); 
     //get all repeatingSection using closest and getting the current repeatingSection's sublings 
     if (other_fights.length === 1) { //check if there is only one element 
      alert("You should atleast have one item"); 
      return; 
     } 
     current_fight.closest('.repeatingSection').slideUp('slow', function() { 
      current_fight.closest('.repeatingSection').remove(); 
      //remove current clicked element's parent using closest 

      // reset fight indexes 
      other_fights.each(function() { 
       resetAttributeNames($(this)); 
      }) 
    }) 
}); 
1

刪除代碼看起來的刪除鏈接的父DIV。然而父母不是重複部分,而是pull-right div。這可以通過使用類似解決:

var current_fight = $(this).closest('div.repeatingSection'); 

Fiddle

0
 // Add a new repeating section 
var attrs = ['for', 'id', 'name']; 
function resetAttributeNames(section) { 
    var tags = section.find('input, label'), idx = section.index(); 
    tags.each(function() { 
     var $this = $(this); 
     $.each(attrs, function(i, attr) { 
     var attr_val = $this.attr(attr); 
     if (attr_val) { 
      $this.attr(attr, attr_val.replace(/_\d+$/, '_'+(idx + 1))) 
     } 
     }) 
    }) 
} 

$('.addFight').click(function(e){ 
     e.preventDefault(); 
     var lastRepeatingGroup = $('.repeatingSection').last(); 
     var cloned = lastRepeatingGroup.clone(true) 
     cloned.insertAfter(lastRepeatingGroup); 
     resetAttributeNames(cloned) 
    }); 

// Delete a repeating section 
$('.deleteFight').click(function(e){ 
     e.preventDefault(); 
     var current_fight = $(this).closest('.repeatingSection'); 
     var other_fights = current_fight.siblings('.repeatingSection'); 
     var fights = $('.repeatingSection') ? $('.repeatingSection').length : 0; 
     if (fights <= 1) { 
      alert("You should atleast have one item"); 
      return; 
     } 
     current_fight.slideUp('slow', function() { 
      current_fight.remove(); 

      // reset fight indexes 
      other_fights.each(function() { 
       resetAttributeNames($(this)); 
      }) 

     }) 


    }); 

,而不是尋找兄弟姐妹,你只需檢查是否有超過1打;而不是刪除按鈕的父div(類拉右),而不是刪除父塊類.repeatingsection