2
我已經使用此示例http://jsfiddle.net/aaki/hMJEy/1/動態地將輸入字段添加到表單,這就像魅力一樣。使用值加載動態添加的輸入字段
這是代碼:
的Javascript:
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
});
HTML:
<form role="form" action="/wohoo" method="POST">
<label>Stuff</label>
<div class="multi-field-wrapper">
<div class="multi-fields">
<div class="multi-field">
<input type="text" name="stuff[]">
<button type="button" class="remove-field">Remove</button>
</div>
</div>
<button type="button" class="add-field">Add field</button>
</div>
</form>
但現在我想從數據庫中的數據,並放入表單中的值。這樣數據就可以更新,然後再次保存在數據庫中。
不幸的是,jQuery是我的弱點,並且一直在努力。任何建議或工作示例?
非常感謝您提前。
不錯!非常整潔的解決方案,我喜歡它在json中的讀取方式。 在你的例子中,你添加了3個名字,反正有3個輸入字段的名字已經在頁面加載時顯示出來了嗎? 非常感謝。 – MrSoolmaan
如果輸入字段數組長度<數據長度,添加輸入字段,希望它有幫助。 - 薛明芳1小時前 –
謝謝大幫忙!我嘗試的東西甚至沒有接近您的解決方案。每天學習新事物。再次感謝! – MrSoolmaan