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



在这里,我需要在无线电按钮中添加默认选择(检查)。

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

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

$ form-> radiobuttonList($型号,'1',$ usernames,array('sapeator'=>"));

您可能可以使用类似的东西:

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默认值,您可以选择将选择哪个收音机。

最新更新