2016-08-13 61 views
0

我想在输入字段中计算我在Laravel中使用的动态形式的索引,但我无法弄清楚如何实现那。如何计数输入名称[0]名称[1]克隆时

这是我当前的代码我使用:

HTML

<div id="language"> 
    <div class="language"> 
     <div class="row"> 
      <div class="form-group col-md-6 col-sm-6"> 
       <input class="form-control" placeholder="Language" name="language[0][name]" type="text"> 
      </div> 
      <div class="form-group col-md-6 col-sm-6"> 
       <select class="form-control" title="Language level" name="language[0][level]"><option value="medium">Medium</option><option value="good">Good</option><option value="perfect">Perfect</option></select> 
      </div> 
      <p><a href="#" class="addsection">Add Section</a></p> 
     </div> 
    </div> 
    </div> 

JS:

//Clone fields 

//define template 
var template = $('#language .language:first').clone(); 

//define counter 
var sectionsCount = 1; 
//add new section 
$('body').on('click', '.addsection', function() { 


    //increment 
    sectionsCount++; 

    //loop through each input 
    var section = template.clone().find(':input').each(function(){ 

     //set id to store the updated section number 
     var newId = this.id + sectionsCount; 

     //update for label 
     $(this).prev().attr('for', newId); 

     //update id 
     this.id = newId; 

    }).end() 

    //inject new section 
     .appendTo('#language'); 
    return false; 


}); 

当我克隆领域,如何才能让我[0]数起来? 当我删除计数下降的字段?

+0

你的问题似乎在目前还不清楚。我什么都听不懂! –

+0

查看答案@ HiI'mFrogatto这就是我所需要的:) – Albert

回答

1

当创建需要为字段更新克隆属性名称,以便值[0]可以是新的元件[1],[2],....

的代码是:

var txtName = this.getAttribute('name'); 

    txtName = txtName.replace('[0]', '[' + (sectionsCount - 1) + ']'); 

    this.setAttribute('name', txtName); 

为了去除一个第一节添加一个删除锚和所述代码是:

$('body').on('click', '.removesection', function() { 
     $(this).closest('.language').remove(); 
    }); 

的片段:

$(function() { 
 
    //Clone fields 
 

 
    //define template 
 
    var template = $('#language .language:first').clone(); 
 

 
    //define counter 
 
    var sectionsCount = 1; 
 
    //add new section 
 
    $('body').on('click', '.addsection', function() { 
 

 

 
    //increment 
 
    sectionsCount++; 
 

 
    //loop through each input 
 
    var section = template.clone().find(':input').each(function(){ 
 

 
     //set id to store the updated section number 
 
     var newId = this.id + sectionsCount; 
 

 
     //update for label 
 
     $(this).prev().attr('for', newId); 
 

 
     //update id 
 
     this.id = newId; 
 
     var txtName = this.getAttribute('name'); 
 
     txtName = txtName.replace('[0]', '[' + (sectionsCount - 1) + ']'); 
 
     this.setAttribute('name', txtName); 
 

 
    }).end() 
 

 
    //inject new section 
 
    .appendTo('#language'); 
 
    return false; 
 

 

 
    }); 
 

 
    // remove current section 
 
    $('body').on('click', '.removesection', function() { 
 
    $(this).closest('.language').remove(); 
 
    }); 
 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 

 
<div id="language"> 
 
    <div class="language"> 
 
     <div class="row"> 
 
      <div class="form-group col-md-6 col-sm-6"> 
 
       <input class="form-control" placeholder="Language" name="language[0][name]" type="text"> 
 
      </div> 
 
      <div class="form-group col-md-6 col-sm-6"> 
 
       <select class="form-control" title="Language level" name="language[0][level]"><option value="medium">Medium</option><option value="good">Good</option><option value="perfect">Perfect</option></select> 
 
      </div> 
 
      <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="addsection">Add Section</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="removesection">Remove Section</a></p> 
 
     </div> 
 
    </div> 
 
</div>

+0

这太好了,谢谢 – Albert

相关问题