如何在单选按钮中设置默认选择 - 活动表单yii

问题描述 投票:0回答:2

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

<?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 } ?>
php yii radio-button
2个回答
1
投票

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

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

1
投票

你可以使用这样的东西:

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->username指定默认值,您才可以选择要选择的无线电。

© www.soinside.com 2019 - 2024. All rights reserved.