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]数起来? 当我删除计数下降的字段?
你的问题似乎在目前还不清楚。我什么都听不懂! –
查看答案@ HiI'mFrogatto这就是我所需要的:) – Albert