本文介紹了Zend Framework - 在選擇框下拉列表中設置“選定"值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在向 Zend_Form 實例添加一個 select 元素,如下所示:
I am adding a select element to a Zend_Form instance as follows:
$user = $form->createElement('select','user')->setLabel('User: ')->setRequired(true);
foreach($users as $u)
{
if($selected == $u->id)
{
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
//*** some way of setting a selected option? selected="selected"
}
else
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
}
我一直在搜索文檔,但找不到將選擇元素的選項預先設置為已選擇"的簡單方法.
I have been searching the docs but cannot find a simple way of pre-setting an option of the select element to 'selected'.
推薦答案
我剛剛想出了如何去做.
I've just worked out how to do it.
必須使用元素的 setValue() 方法:
You have to use the setValue() method of the element:
$user = $form->createElement('select','user')->setLabel('User: ')->setRequired(true);
foreach($users as $u)
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
$user->setValue($selected); //$selected is the 'value' of the <option> that you want to apply selected="selected" to.
這篇關于Zend Framework - 在選擇框下拉列表中設置“選定"值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!