2017-12-18 275 views
0

這裏我需要在單選按鈕(選中)中添加默認選擇。如何在單選按鈕中設置默認選擇 - 活動形式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 } ?> 
+0

你爲什麼不使用radioList? – madalinivascu

回答

1

您需要使用以下方法,其中1是您選擇的ID。只有通過指定$model->username的默認值,你可以選擇電臺將被選中

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

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

在現有代碼中沒有其他選項可供選擇默認值? – user3686600

+0

你能告訴我你正在使用哪個Yii版本嗎? – dipmala

1

你或許可以用這樣的事情。

+0

我已在我的代碼中添加,但添加後沒有默認選擇 – user3686600

+0

您使用的是什麼條件? – mega6382

+0

我的意思是,你究竟是如何實現這個的? – mega6382

相關問題