【问题标题】:how to set default selection in radio button - Active form yii如何在单选按钮中设置默认选择 - 活动表单 yii
【发布时间】:2017-12-18 14:01:59
【问题描述】:

这里我需要在单选按钮中添加默认选择(选中)。

<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>    
<?= $form->field($model, 'email') ?>
<?php if(!empty($usernames)){ 
    echo '<label>Select Any Username To Which You Want To Login</label><div class="all_username">';
    foreach ($usernames as $key => $value) { ?>
        <?= $form->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username']) ?>
    <?php } ?>
    </div>
    <?= $form->field($model, 'password')->passwordInput() ?>
<?php } ?>

【问题讨论】:

  • 你为什么不用radioList?

标签: php yii radio-button


【解决方案1】:

您需要使用以下方法,其中 1 是您选择的 ID。

$form->radioButtonList($model,'1', $usernames, array('separator'=>"" ));

【讨论】:

  • 现有代码中没有其他选项可以选择默认值吗?
  • 您能告诉我您使用的是哪个 Yii 版本吗?
【解决方案2】:

你可能会使用这样的东西:

foreach ($usernames as $key => $value) { 
    if(/*somecondition*/){
        $model->username = $value['username'];
    }?>
    <?= $form->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username']) ?>
<?php } ?>

只有为$model-&gt;username 分配默认值,您才能选择要选择的收音机。

【讨论】:

  • 我已经在我的代码中添加了,但是添加后没有默认选择
  • 你用了什么条件?
  • 我的意思是,您具体是如何实现的?
  • foreach ($usernames as $key => $value) { if($key == 0) $model->username = $value['username'];?> = $form ->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username' ]) ?>
猜你喜欢
  • 2017-01-03
  • 2021-05-13
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 2015-12-10
  • 1970-01-01
  • 2013-07-20
相关资源
最近更新 更多