Php Zend Form无线电默认值已选中

Php Zend Form无线电默认值已选中,php,zend-framework,radio-button,zend-form,Php,Zend Framework,Radio Button,Zend Form,我有下一个单选按钮组: $enabled = $this->createElement('radio', 'enabled') ->setLabel('Enabled') ->setMultiOptions(array('1'=>'yes', '0'=>'no')) ->setValue($rank_values['enabled'])

我有下一个单选按钮组:

$enabled = $this->createElement('radio', 'enabled')
                ->setLabel('Enabled')
                ->setMultiOptions(array('1'=>'yes', '0'=>'no'))
                ->setValue($rank_values['enabled'])
                ->setAttrib('id', 'enabled')
                ->setAttrib('class', $action . '_enabled')
                ->setSeparator('');
我怎样才能设置一台检查过的收音机?现在,当我打开脚本时,没有选择收音机。我想选择“是”。怎么做

谢谢。

请使用以下选项:

->setAttrib("checked","checked")
因此,您的完整代码如下所示:

$enabled = $this->createElement('radio', 'enabled')
            ->setLabel('Enabled')
            ->setMultiOptions(array('0'=>'no', '1'=>'yes'))
            ->setAttrib("checked","checked")
            ->setValue($rank_values['enabled'])
            ->setAttrib('id', 'enabled')
            ->setAttrib('class', $action . '_enabled')
            ->setSeparator('');

[编辑]使用
setValue

您也可以使用以下选项:

->setValue('1')

这将检查由值
1
表示的选项,该值为
yes

yes。我已经用jQuery解决了这个问题:

jQuery(document).ready(function(){
    var $radios = $('input:radio[name=enabled]');
    if($radios.is(':checked') === false) {
        $radios.filter('[value=1]').attr('checked', true);
    }
});
这要容易得多:)


我发现,如果您有一个过滤器集,那么
->setvalue('X')
就不起作用

我删除了addFilter('StringToLower')
并添加了
->setSeparator(“”)->setValue('N')


处理了一个问题

如果有人想知道,我正在使用数组表示法声明我表单中的所有元素,在zend framework 2中,要在单选按钮中选择默认选项,您必须添加属性值,并使其具有默认要选择的值\u选项的键:

// Inside your constructor or init method for your form //
$this->add(
        [
            'type'       => 'Radio',
            'name'       => 'some_radio',
            'options'    => [
                'value_options' => [
                    'opt1' => 'Radio option 1',
                    'opt2' => 'Radio option 2'
                ]
            ],
            'attributes' => [
                'value' => 'opt1' // This set the opt 1 as selected when form is rendered
            ]
        ]
    );

我发现一些示例有点混乱,因为它们在值选项(0,1)中使用数字键,所以当我看到“value”=>1时,我不清楚这是值选项数组中的键。希望这对某人有所帮助。

根据手册,如果要使用数组表示法,您可以这样做:


是的,如果我们只有一个元素,它就可以工作。我在广播组有两个成员。是的。它选择“多重选项”中的最后一个元素。它选择了
no
@Alexander,这就是为什么我在我的完整代码中替换了
yes
no
。仔细看。@shamittomar,我认为对所有元素进行
setAttrib
的“检查”和“检查”就是设置该属性,所以最后一个元素通常默认为选中的答案。这将是工作。但是我需要一个
yes
第一个值。印度节目。。。很抱歉
// Inside your constructor or init method for your form //
$this->add(
        [
            'type'       => 'Radio',
            'name'       => 'some_radio',
            'options'    => [
                'value_options' => [
                    'opt1' => 'Radio option 1',
                    'opt2' => 'Radio option 2'
                ]
            ],
            'attributes' => [
                'value' => 'opt1' // This set the opt 1 as selected when form is rendered
            ]
        ]
    );
 $this->add(
        [
            'name'       => 'someRadioMethod',
            'type'       => 'radio',
            'options' => [
                'label' => 'Some descriptive label',
                'value_options' => [
                    [
                        'value' => '1',
                        'label' => 'Label for 1',
                        'selected' => true,

                    ],
                    [
                        'value' => '2',
                        'label' => 'Label for 2',
                        'selected' => false,

                    ]
                ],
            ],
        ]
    );