php Zend Framework - 在选择框下拉列表中设置“选定”值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1588272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:11:50  来源:igfitidea点击:

Zend Framework - Set 'selected' value in select box dropdown list

phpzend-frameworkzend-formhtml-select

提问by Tom

I am adding a select element to a Zend_Form instance as follows:

我正在向 Zend_Form 实例添加一个 select 元素,如下所示:

  $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'.

我一直在搜索文档,但找不到将选择元素的选项预先设置为“已选择”的简单方法。

回答by Tom

I've just worked out how to do it.

我刚刚想出了怎么做。

You have to use the setValue() method of the element:

您必须使用元素的 setValue() 方法:

$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.

回答by Tony

$form->addElement('select','foo',
array(
        'label'        => 'ComboBox (select)',
        'value'        => 'blue',
        'multiOptions' => array(
            'red'    => 'Rouge',
            'blue'   => 'Bleu',
            'white'  => 'Blanc',
        ),
    )
);

As above, you can use 'value' => 'blue' for making 'blue' => 'Bleu' selected.

如上所述,您可以使用 'value' => 'blue' 来选择 'blue' => 'Bleu'。

I hope this will help you..

我希望这能帮到您..

回答by Onshop

In Zend Framework 2 set the 'value' attribue. For example to default the Select to 'Yes':

在 Zend Framework 2 中设置“值”属性。例如默认选择为“是”:

    $this->add( array(
        'name'     => 'isFlexible',
        'type'     => 'Select',
        'options'  => array(
             'label'            => 'Is it flexible?'
            ,'label_attributes' => array( 'placement' => 'APPEND')
            ,'value_options'    => array(
                    ''  => 'Select Below',
                    '0' => 'No',
                    '1' => 'Yes',
                    '2' => 'N/A',
            ),
        ),
        'attributes' => array(
            'id'     => 'is_flexible',
            'value'  => 1,
        ),
    ));

回答by opHASnoNAME

i think this should work:

我认为这应该有效:

$form->setDefault('user', 'value'); // Set default value for element

回答by Yazid Erman

The mentioned solutions won't work for Zend Framework 2, For those who use Zf2, I suggest using the following instruction to set a default value:

上述解决方案不适用于 Zend Framework 2,对于使用 Zf2 的人,我建议使用以下说明设置默认值:

    $formX->get('<Select element Name>')->setValue(<the id of the selected item>);

回答by Nuar Haruha

To set default values you can try both setDefault or populate

要设置默认值,您可以同时尝试 setDefault 或 populate

$form->populate( $array_keypair_values );

$form->populate( $array_keypair_values );

回答by Gokul Shinde

I just try following code to show drop-down value as selected from controller and it work fine.

我只是尝试以下代码来显示从控制器中选择的下拉值,它工作正常。

$user->setValue($value); //$value is the 'value' of the and $user is the element of from.

$user->setValue($value); //$value 是 the 的“value”,$user 是 from 的元素。