yii2:使复选框被选中

2022-01-04 00:00:00 php html yii active-form

我正在使用 Yii2 框架,我想生成这样的 html 代码

在使用 ActiveForm 的视图中.

我试过了

echo $form->field($model, 'order')->checkBox(['label' => ..., 'uncheck' => null, 'checked' => true]);

以及

echo $form->field($model, 'order')->checkBox(['label' => ..., 'uncheck' => null, 'checked' => 'checked']);

但是生成的 html 代码中没有出现所需的字符串checked".

奇怪的是,如果我用selected"代替checked"

echo $form->field($model, 'order')->checkBox(['label' => ..., 'uncheck' => null, 'selected' => true]);

然后生成的html代码包含属性selected":

那么,如何为属性为checked"的复选框生成 html 代码?

解决方案

我猜这个复选框只有在 $model->order 属性取 true 值时才会被选中如果它有 false(0nullfalse 等)值 - 字段将不被选中.

I am using Yii2 framework and I'd like to generate an html code like this

<input type="checkbox" id="queue-order" name="Queue[order]" value="1" checked>

in a view which uses 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.

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":

<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"?

解决方案

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.

相关文章