php - yii2 : make checkbox to be checked

标签 php html yii active-form

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

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

在使用 ActiveForm 的 View 中。

我试过了

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

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

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

最佳答案

我猜只有当 $model->order 属性取 true 值并且它有 false 时才会选中此复选框(0nullfalse 等)值 - 字段将被取消选中。

关于php - yii2 : make checkbox to be checked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23981627/

相关文章:

php - 根据两列列出表

php - 将此 php 正则表达式转换为 javascript

php - 在 Symfony 测试中控制依赖关系

python - 使用 html5lib 验证 HTML 片段

mysql - yii 尝试获取非对象的属性

php - Yii 复合主键与 isNewRecord

PHP用外键删除记录后,检查int表

javascript - 以特定网格布局显示图像 : ejs

javascript - jQuery 无法在 Bootstrap 模式下工作

Javascript 日期减去 30 分钟/完整日历调整大小错误