如何将特殊跨度添加到 addDisplayGroup 字段集

How to add special span to addDisplayGroup fieldset

这段代码需要修改什么地方

$this->addDisplayGroup($groupInputs, 'group_'.$i, 
                    array('attribs'=>array('class'=>'fieldset_group')));

在字段集中添加新的 html 标签,例如:

<span class="delete_fieldset" onclick="foo()">[X]</span>

或者如果可能的话:

 <span class="delete_fieldset_$i" onclick="foo($i)">[X]</span>

我不确定是否理解你的问题。
也许这段代码可以帮助你:

// add a Zend_Form_Element_Note (Zend 1.12)
$newtag = new Zend_Form_Element_Note('span_elt', array('value' => '<span class="delete_fieldset_'. $i .'" onclick="foo(' . $i . ')">[X]</span>'));

// Remove decorators to have only span tag
$newtag->removeDecorator('HtmlTag');
$newtag->removeDecorator('Label');

// add $newtag in your code
$this->addDisplayGroup(array_merge(array($newtag), $groupInputs), 
                       'group_'.$i, 
                       array('attribs'=>array('class'=>'fieldset_group')));

希望对你有所帮助

另一种方法是像这样使用 javascript:

var attr_fieldset = "";
var num_fielset = 0;
var elt_fieldset = null;

// get all fieldsets
var list_fieldset = document.getElementsByClassName('fieldset_group');

for (var i = 0; i < list_fieldset.length; i++) {
    // to get one fieldset
    elt_fieldset = list_fieldset[i];
    // to get $i 
    attr_fieldset = elt_fieldset.id.split("_");
    num_fielset = attr_fieldset[1]; // (num_fielset = $i)

    // create span element
    var new_span = document.createElement("span");
    new_span.className = 'delete_fieldset_' + num_fielset;
    new_span.setAttribute('onclick', 'foo(' + num_fielset + ')');
    new_span.innerHTML = '[X]';
    // span element is the first child of the fieldset
    elt_fieldset.insertBefore(new_span, elt_fieldset.firstChild);
}

如果您想在字段集的其他元素之后使用 span 元素,请替换

elt_fieldset.insertBefore(new_span, elt_fieldset.firstChild);

来自

elt_fieldset.appendChild(new_span);

希望对你有所帮助