php Zend Form Radio 默认选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3659620/
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
Zend Form Radio Default Checked
提问by Alex Pliutau
I have the next radio button group:
我有下一个单选按钮组:
$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('');
How can I set a checked radio? Now, when I open my script, there are not selected radio. I want to select 'yes'. How?
如何设置选中的收音机?现在,当我打开我的脚本时,没有选择收音机。我想选择“是”。如何?
Thank you.
谢谢你。
回答by Fribu - Smart Solutions
it is much more easier :)
它更容易:)
$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('')
->setValue("1");
回答by markdrake
In case somebody wonders, I'm using the array notation to declare all my elements in my forms and in zend framework 2 to have a default option selected in a radio button you have to add the attribute value and make it have the key of the value_options you want to be selected by default:
如果有人想知道,我正在使用数组表示法来声明我的表单和 zend 框架 2 中的所有元素,以便在单选按钮中选择默认选项,您必须添加属性值并使其具有您希望默认选择的 value_options:
// 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
]
]
);
I found some examples a little confusing because they were using numeric keys in the value options (0, 1) so when I saw 'value' => 1 it wasn't obvious for me that this was the key in the value_options array. Hope this helps someone.
我发现一些示例有点令人困惑,因为它们在值选项 (0, 1) 中使用了数字键,所以当我看到 'value' => 1 时,对我来说这是 value_options 数组中的键并不明显。希望这可以帮助某人。
回答by HappyCoder
According to the manual, you would do it this way if you were to use array notation: link to manual
根据手册,如果您要使用数组表示法,您会这样做:链接到手册
$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,
]
],
],
]
);
回答by shamittomar
Use this:
用这个:
->setAttrib("checked","checked")
So that your complete code looks like this:
这样您的完整代码如下所示:
$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('');
[EDIT]Using setValue:
[编辑]使用setValue:
You can alternatively use this:
你也可以使用这个:
->setValue('1')
This will check the option represented by value 1which is yes.
这将检查由值表示的选项1,即yes。
回答by Maxui
I found that if you have a filter set then ->setvalue('X')does not work.
我发现如果您设置了过滤器,则->setvalue('X')不起作用。
I removed ->addFilter('StringToLower')
and added ->setSeparator('')->setValue('N');
我删除->addFilter('StringToLower')
并添加->setSeparator('')->setValue('N');
Worked a treat
辛苦了
回答by Alex Pliutau
Yeah. I have resolved it using jQuery:
是的。我已经使用 jQuery 解决了它:
jQuery(document).ready(function(){
var $radios = $('input:radio[name=enabled]');
if($radios.is(':checked') === false) {
$radios.filter('[value=1]').attr('checked', true);
}
});

