我在表单上有动态元素和字段。
JS:
<script>
function hapus(i){
$("#field"+i).remove();
}
</script>
HTML:
<div class="fieldwrapper" id="field1">
<select class="fieldtype" id="labensmittel1" name="data[Tagebuch][labensmitell1]" style="width:160px;">
<option value="data">data</option>
</select>
<input type="text" value="" id="qty1" name="data[Tagebuch][qty1]" class="fieldname" />
<input type="text" value="" id="labendata1" name="data[Tagebuch][labendata1]" class="fieldname" />
<input type="button" class="hapus1" value="-" style="width:20px;" id="hapus1" onclick="hapus(1);" />
</div>
<div class="fieldwrapper" id="field2">
<select class="fieldtype" id="labensmittel2" name="data[Tagebuch][labensmitell2]" style="width:160px;">
<option value="data">data</option>
</select>
<input type="text" value="" id="qty2" name="data[Tagebuch][qty2]" class="fieldname" />
<input type="text" value="" id="labendata2" name="data[Tagebuch][labendata2]" class="fieldname" />
<input type="button" class="hapus2" value="-" style="width:20px;" id="hapus2" onclick="hapus(1);" />
</div>
<div class="fieldwrapper" id="field3">
<select class="fieldtype" id="labensmittel3" name="data[Tagebuch][labensmitell3]" style="width:160px;">
<option value="data">data</option>
</select>
<input type="text" value="" id="qty3" name="data[Tagebuch][qty3]" class="fieldname" />
<input type="text" value="" id="labendata3" name="data[Tagebuch][labendata3]" class="fieldname" />
<input type="button" class="hapus3" value="-" style="width:20px;" id="hapus3" onclick="hapus(3);" />
</div>
如何按名称重新排序字段?例如,如果我移除field2
div id='field3'
,则会将其更改为div id='field2'
,然后id="labensmittel3"
和name="data[Tagebuch][labensmitell3]"
中的选择和输入字段ID和名称将移至{ {1}}和id="labensmittel2"
由于
答案 0 :(得分:4)
删除后发生的基本迭代如何重置所有id:
var fields = $('.fieldwrapper');
var count = 1;
$.each(fields, function() {
$(this).attr('id','field' + count);
count++;
});