使用集合時,指定allow_add
或allow_remove
不會告知ZF創建適當的按鈕,只是該集合可能包含任意數量的元素(最小值由count
指定)。
將集合添加到表單後,您還需要添加一個按鈕,單擊該按鈕時會調用一個函數以添加基於模板的其他元素。
從手冊:
<button onclick="return add_category()">Add a new category</button>
和
<script>
function add_category() {
var currentCount = $('form > fieldset > fieldset').length;
var template = $('form > fieldset > span').data('template');
template = template.replace(/__index__/g, currentCount);
$('form > fieldset').append(template);
return false;
}
</script>
要添加刪除按鈕,改變上述功能的按鈕添加到模板中,並創建一個刪除功能:
<script>
function add_category() {
var currentCount = $('form > fieldset > fieldset').length;
var template = $('form > fieldset > span').data('template');
template = template.replace(/__index__/g, currentCount)
.replace('</fieldset>', '<button onclick="return remove_category(this)">Remove</button></fieldset>');
$('form > fieldset').append(template);
return false;
}
function remove_category(el) {
$(el).parent().remove();
}
</script>
嗨!您的問題在官方文檔中有適當的答案:https://framework.zend.com/manual/2.4/en/modules/zend.form.collections.html#adding-new-elements-dynamically。要麼你沒有看到,要麼你有更具體的問題,所以請添加詳細信息,以幫助我們更多! –