1
你好我試圖從本教程中得到啓發後添加新的表單字段和刪除表單字段 - http://bootsnipp.com/snipps/dynamic-form-fieldsjQuery的動態添加表格字段,刪除表格字段
我目前的問題是刪除並重新設置所有的值按時間順序排列。
<input type="hidden" name="count" value="1" />
<div class="control-group" id="fields">
<label class="control-label" for="field1">Nice Multiple Form Fields</label>
<div class="controls" id="profs">
<div class="input-append">
<input autocomplete="off" class="span3" id="field1" name="prof1" type="text" placeholder="Type something (it has typeahead too)" data-provide="typeahead" data-items="8"
data-source='["Aardvark","Beatlejuice","Capricorn","Deathmaul","Epic"]'/><button id="b1" onClick="addFormField()" class="btn btn-info" type="button">+</button>
</div>
<br /><small>Press + to add another form field :)</small>
</div>
</div>
的Javascript: -
var next = 1;
function addFormField(){
var addto = "#field" + next;
next = next + 1;
var newIn = '<br /><br /><input autocomplete="off" class="span3" id="field' + next + '" name="field' + next + '" type="text" data-provide="typeahead" data-items="8"><button id="b1" onClick="$(this).parent().remove();" class="btn btn-info" type="button">+</button>';
var newInput = $(newIn);
$(addto).after(newInput);
$("#field" + next).attr('data-source',$(addto).attr('data-source'));
$("#count").val(next);
}
父元素是越來越刪除,但下一個計數器不能正確復位。在隱藏和所有新創建的窗體: -
只添加演示: - http://bootsnipp.com/snipps/dynamic-form-fields
添加刪除演示與錯誤: - http://jsfiddle.net/6dCrT/2/
有人可以幫我請。
感謝
但字段值沒有得到復位。添加3個字段並刪除2個其他字段後..下一個字段仍然帶有數字'field4':( –
@JohnyBravo爲什麼你想要改變元素屬性,如ID,名字?你仍然可以解析父div的所有輸入內 – Unknown
請原諒我的noobness,你能解釋一下你的意思嗎? –