问题描述
我想在我的表单中添加一个简单的复选框:
I would like to add a simple check box to my form:
$element = new Zend_Form_Element_Checkbox('dont');
$element->setDescription('Check this box if you don\'t want to do this action.');
$form->addElement($element);
但是,这是html的样子:
However, this is what the html looks like:
<dt id="dont-label"> </dt>
<dd id="dont-element">
<input type="hidden" name="dontAttach" value="0">
<input type="checkbox" name="dontAttach" id="dontAttach" value="1">
<p class="description">Don't attach a bulletin. I only want to send an email.</p>
</dd>
问题是我使用jQuery来隐藏所有的DT / DD在DT中的& nbsp;
标签和DD中的隐藏元素(因此我的html将验证,隐藏元素不占用页面上的空间)。有没有办法使用 Zend_Form_Element_Checkbox
而不必显示隐藏的输入元素?我宁愿不要用我的jQuery代码添加更多的警告,但如果我必须这样做。
The problem with this is that I'm using jQuery to hide all the DT/DDs that have a label of
inside the DT and a hidden element inside the DD (so my html will validate and the hidden elements don't take up space on the page). Is there a way to use a Zend_Form_Element_Checkbox
without having to display a hidden input element? I'd rather not mess with my jQuery code to add more caveats, but I will if I have to.
显然,我不能/不应该删除checkbox元素之前的隐藏元素。所以这里是我的jQuery代码隐藏所有隐藏表单元素显示在页面上:
Apparently, I can't/shouldn't remove the hidden element before the checkbox element. So here's my jQuery code to hide all the hidden form elements from being displayed on a page:
//fix zf hidden element from displaying
$('input[type=hidden]').filter(function() {
var noLabel = $(this).closest('dd').prev('dt').html() === ' ';
var onlyChild = $(this).is(':only-child');
if (noLabel && onlyChild) {
return true;
}
return false;
}).each(function() {
$(this).closest('dd').hide()
.prev('dt').hide();
});
推荐答案
要更改表单元素的呈现方式,可以使用,可以用
To change the way a form element is rendered, you can use the decorators, which can be modified with
// Overwrite existing decorators with this single one:
$element->setDecorators(array('Composite'));
有关所有默认装饰器的列表,请查看;对于表单字段使用的装饰器列表,您可以看到。
For a list of all the default decorators, you look at standard decorators; for a list of the decorators used by the form fields, you can see standard form elements.
在我看来,隐藏表单元素是从Zend添加一个精确的目的,删除它(如果这是可能的)可能会导致一些问题。我的第一个想法是,Zend使用隐藏形式来检查值是否已经改变,或者验证是否真的从Zend生成了这个假设(这个假设似乎不太合理)。
It seems to me that the hidden form elements is added from Zend with a precise purpose, and removing it (if that is even possible) could cause some problems. My first thought is that Zend uses that hidden form to check if the value has been changed, or to verify if the from has been really generated from Zend (this hypothesis seems less plausible).
这篇关于Zend表单:复选框元素显示为隐藏字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!