如何将 Yii2 的 ActiveForm 复选框设置为选中状态? [英] How to set Yii2's ActiveForm checkbox in checked state?

查看:21
本文介绍了如何将 Yii2 的 ActiveForm 复选框设置为选中状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Yii2 ActiveForm 复选框控件寻找已检查"状态的简单解决方案,但我找不到如何设置它的解决方案.我在文档中找不到任何示例.

I'm looking for a simple solution for a "checked" state for the Yii2 ActiveForm checkbox control and I can't find a solution how to set it. I cant find any examples in the documentation.

我试图操纵渲染代码

<?= $form->field($model, 'name')->checkbox()->label('Hi'); ?>

但似乎我需要修改 ActiveForm 本身.如何让复选框默认选中?

But it seems I need to modify the ActiveForm itself. How to make checkbox checked by default?

推荐答案

看,在我的代码中 status 是 0 或 1.

See, in my code status is either 0 or 1.

所以,对于选中的复选框,我声明 <?php $model->status = 1;?>

So, for checked checkbox., I declared <?php $model->status = 1; ?>

<?php $model->status = 1; ?>
<?= $form->field($model, 'status')->checkbox()->label('Hi'); ?>

例如,如果复选框名称有 2 个值,即 PHP 或 Java.然后,将其声明为 <?php $model->name = 'Java';?> 用于将 Java 设为 checked

For example, if you are having 2 values for checkbox name i.e. PHP or Java. Then, declare it as <?php $model->name = 'Java'; ?> for getting Java as checked

//Your Code

<?php $model->name = 'Java'; ?>
<?= $form->field($model, 'name')->checkbox()->label('Hi'); ?>

这篇关于如何将 Yii2 的 ActiveForm 复选框设置为选中状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆