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

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

yii2: make checkbox to be checked

phphtmlyiiactive-form

提问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->orderproperty take truevalue and if it has false(0or nullor falseetc) value - field will be unchecked.

我猜这个复选框只有在$model->order属性true取值并且它有false0nullfalse等)值时才会被选中 - 字段将被取消选中。

回答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']);