php yii2:使复选框被选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23981627/
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
yii2: make checkbox to be checked
提问by Andrew
I am using Yii2 framework and I'd like to generate an html code like this
我正在使用 Yii2 框架,我想生成这样的 html 代码
<input type="checkbox" id="queue-order" name="Queue[order]" value="1" checked>
in a view which uses ActiveForm.
在使用 ActiveForm 的视图中。
I've tried
我试过了
echo $form->field($model, 'order')
->checkBox(['label' => ..., 'uncheck' => null, 'checked' => true]);
as well as
也
echo $form->field($model, 'order')
->checkBox(['label' => ..., 'uncheck' => null, 'checked' => 'checked']);
but desired string "checked" does not appear in the generated html code.
但生成的 html 代码中没有出现所需的字符串“checked”。
Strangely enough, if I substitute "checked" with "selected"
奇怪的是,如果我用“选中”替换“选中”
echo $form->field($model, 'order')
->checkBox(['label' => ..., 'uncheck' => null, 'selected' => true]);
then generated html code contains attribute "selected":
然后生成的 html 代码包含属性“selected”:
<input type="checkbox" id="queue-order" name="Queue[order]" value="1" selected>
So, how can I generate html code for a checkbox with attribute "checked"?
那么,如何为属性为“已选中”的复选框生成 html 代码?
采纳答案by Goodnickoff
I guess this checkbox will be checked only if $model->order
property take true
value and if it has false
(0
or null
or false
etc) value - field will be unchecked.
我猜这个复选框只有在$model->order
属性true
取值并且它有false
(0
或null
或false
等)值时才会被选中 - 字段将被取消选中。
回答by shivansh
if your are setting an external value in checkbox.
如果您在复选框中设置外部值。
<?php $model->order = "02256"; ?>
<?= $form->field($model, "order")->checkbox(['value' => "02256"]); ?>
回答by Maxim Colesnic
echo $form->field($model, 'Status')->checkbox(['uncheck' => 'Disabled', 'value' => 'Active']);