douren6035 2016-01-15 13:11
浏览 241
已采纳

ActiveDropDownList中的默认值

I've inherited a piece of code from an old developer and currently trying to figure out the method behind how it submits.

I'm currently trying to get a quick fix in place where I hide one of his fields to get the form submitting, the issue I'm having is that I can't seem to set the dropdownlist to a default value, which should be United Kingdom, with the id 826 in the database.

echo CHtml::ActiveDropDownList($address, 'country_id', CHtml::listData(Country::model()->findAll(), 'id', 'name', 'continent'), array(
    'class' => 'col-md-5 hidden',
    'prompt' => 'Select Country',
    'label' => 'something here',
    'ajax' => array(
        'type' => 'POST',
        'url' => CController::createUrl('/user/UpdateRegions'),
        'dataType' => 'json',
        'data' => array('country_id' => 'js:this.value', 'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
        'success' => 'function(data) {
                            $("#Address_country_region_id").html(data.country_id);
                            $("#Address_country_region_id").removeClass(\'hidden\');

                            if($("#venue_id").val() === "") {
                                $("#Address_country_region_id").addClass(\'hidden\');
                                }
                        }',
)));

$path = CController::createUrl('/admin/user/UpdateRegions');
$id = $address->id;
// On Load
Yii::app()->clientScript->registerScript('ready', '
    $.ajax({
        type: \'POST\',
        dataType : \'json\',
        data: {\'country_id\': $(\'#Address_country_id\').val(), \'update_id\' : "' . $id . '"},
        url: "' . $path . '",
        success: function(data){
                    $(\'#Address_country_region_id\').html(data.country_id);
                }
    })
');

How do I get this dropdownlist pointing towards the country_id of 826 as the page loads so I can hide the field and pass the validation of the form.

regards.

  • 写回答

2条回答 默认 最新

  • doubi8512 2016-01-15 13:53
    关注

    this code you pasted does this:

    the first part renders a dropdown which does an ajax request on change to /user/UpdateRegions to fill the dependent dropdown for the regions.

    the second part does also an ajax request when the page is "ready". So that the region select is filled when the page is ready.

    Your script should output the id 826 if you printout the current set id. If not the value isn't correctly set to the address model.

    echo $address->country_id;
    

    If you want to set the default value you can do this at multiple places. e.g. in the controller 'before' the actual submitted value is set or in the model code itself (depends on your code e.g. if you have an additional form model which is extended from your activerecord model).

    The correct line in your controller could be before something like this:

     $address->country_id= 826;
     before one of these lines in your controller
    
     $address->attributes=Yii::app()->request->getQuery(get_class($address));
     or 
     $address->attributes=Yii::app()->request->getPost(get_class($address));
     or 
     $address->attributes=$_POST['address']; // or $_GET
    

    If you have difficulties to find the correct position post some controller code.

    I guess you could find some help also here Yii 1.1: An Easy Solution for Dependent dropDownList Using AJAX

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)