2014-11-01 19:12:07 8393次浏览 2条回答 1 悬赏 2 金钱

单选按键,下拉框创建及默认选择某个值,怎么写

最佳答案

  • nswindlights 发布于 2014-11-04 16:47 举报

    比如要选择国家的一个下拉列表:
    controller代码:

    ...
    $model = new User;
    $model->country_id = 1;
    $countrys = array(1=>'中国',2=>'美国');
    ...
    

    vew代码

    use yii\widgets\ActiveField;
    
    $field = new ActiveField(['model']=>$model,'attribute '=>'country_id');
    echo $field->dropDownList($countrys);
    

    这只是单独生成一个下拉列表

    大致就是这样了

    还有就是形成一个整的表单(用yii\widgets\ActiveForm):
    controller差不多。view代码如下

    use yii\widgets\ActiveForm;
    
    $form = ActiveForm::begin();
    echo $form->field($model, 'country_id')->dropDownList($countrys);
    
    1 条回复
    回复于 2014-11-04 17:09 回复

    还有个用的是yii\helpers\Html
    user yii\helpers\Html
    echo Html::dropDownList('country_id',1,$countrys) ;//第一个参数是元素的name 第二个就是选中的值 第三个就是下拉列表的数据。
    至于单选按钮就是用checkboxList()方法了。把dropDownList换成checkboxList

您需要登录后才可以回答。登录 | 立即注册
qq260101081
见习主管

qq260101081 广州

注册时间:2014-10-30
最后登录:2017-09-25
在线时长:13小时20分
  • 粉丝4
  • 金钱40
  • 威望10
  • 积分270

热门问题