2010-09-15 41 views
1

我想id屬性添加到標籤字段在Zend的表單元素,但在裝飾性設置「身份證」 =>「namelabel」時,它爲='namelabel '在標籤,而不是添加ID =的屬性' namelabel」添加id屬性的Zend_Form_Decorator_Label

$name = new Zend_Form_Element_Text('name'); 
$name->setLabel('Business Name:'); 
$name->addDecorator('Label',array('class'=>'form_label', 'id'=>'namelabel')); 
$name->addDecorator('HtmlTag',array('tag'=>'span','class'=>'form_inputs')); 
$name->setOrder(1); 
$name->size='40'; 

呈現

<label for="namelabel" class="form_label optional">Business Name:</label> 

時,我希望它渲染

<label id="namelabel" for="name" class="form_label optional">Business Name:</label> 

這甚至可能嗎?

回答

2

ZF做的是正確的,因爲標籤中的for應該與input中的id相同。所以你不應該改變這是不同的東西。

你試過:

$name->addDecorator('Label',array('class'=>'form_label', 'id'=>'name')); 

更重要的是,你爲什麼要這麼做? OP響應後

編輯

for應該是這個夠獨特,你可以改變你的ZF代碼的下方,因爲你不需要設置id

$name = new Zend_Form_Element_Text('zf_element_name'); 
$name->setLabel('Business Name:'); 
$name->addDecorator('Label',array('class'=>'form_label')); 
$name->addDecorator('HtmlTag',array('tag'=>'span','class'=>'form_inputs')); 
$name->setOrder(1); 
$name->size='40'; 

然後你的JavaScript,如果你使用jQuery你可以做到這一點

$("label[for='zf_element_name']").html("Some new Label text"); 
+0

我明白的應該是相同的輸入的id,這就是爲什麼我仍然需要for屬性。我想在我的標籤標籤上使用唯一的id屬性的原因是,我可以在javascript調用上更改innerHTML – Rob 2010-09-16 21:50:54

+0

我已經爲您編輯了我的答案 – 2010-09-16 22:09:58

+0

非常感謝您的回覆。不幸的是,我使用原型而不是jQuery,原型中是否有類似的功能? – Rob 2010-09-17 02:27:45