【发布时间】:2016-08-14 05:12:09
【问题描述】:
我正在尝试以我在 Laravel 中使用的动态形式计算输入字段上的索引,但我不知道如何实现。
这是我当前正在使用的代码:
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 这就是我需要的:)
标签: javascript jquery laravel laravel-5