php Zend 表单复选框已选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3558586/
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 Checkbox checked
提问by Alex Pliutau
How can I make checkbox checked in Zend Framework depending on the variable value?
For example, I have $some_value = 'yes';
- checkbox should be checked, unchecked otherwise. Thanks.
如何根据变量值在 Zend Framework 中选中复选框?例如,我有$some_value = 'yes';
- 复选框应该被选中,否则不选中。谢谢。
回答by Jake N
Thought I would have a go at this, your question is badly written, but I will do my best. There are are a few solutions below, I have not tested them all. Give them a try.
以为我会尝试一下,您的问题写得很糟糕,但我会尽力而为。下面有几个解决方案,我没有全部测试过。给他们一个尝试。
Example 1
示例 1
// create your checkbox
$checkbox_element = new Zend_Form_Element_Checkbox("mycheckbox");
// Set the value to 1 if needed
if($some_value == "yes")
{
$checkbox_element->setValue(1);
}
Example 2
示例 2
// Check if value is set
if($some_value == "yes")
{
// Create checkbox element and Set the attribute 'checked' to 'checked'
$checkbox_element = new Zend_Form_Element_Checkbox("mycheckbox", array("checked" => "checked");
}
else
{
// Othrewise create the checkbox which is not checked
$checkbox_element = new Zend_Form_Element_Checkbox("mycheckbox");
}