Yii 1.1.3 setting selected value of dependent drop

2019-08-11 07:53发布

问题:

Previously I've created a question where I was wondering how to set selected value for dependent dropdown. That was easy, man adviced me just set second parameter.

    echo CHtml::dropDownList('OpenLessons[Building]', $buildingClassID, $buildingList,array(
        'ajax' => array(
        'type'=>'POST', 
        'url'=>CController::createUrl('ajax/floorList'),
        'update'=>'#OpenLessons_Floor', 
        ))); 
    echo CHtml::dropDownList('OpenLessons[Floor]',$model->Class_ID, array(),array(
        'ajax' => array(
        'type'=>'POST', //request type  
        'url'=>CController::createUrl('ajax/roomList'),
        'update'=>'#OpenLessons_Class_ID',
        )));
    echo CHtml::dropDownList('OpenLessons[Class_ID]',$model->Class_ID, array());

where $buildingClassId is value of select. So that works fine for first select. But how can I set the same for dependent ones? When I look at it's code it looks like this:

<select name="OpenLessons[Floor]" id="OpenLessons_Floor">
</select>

I expected it to have some options, because I set default value for first one. So even if I put as second parameter of seconddropdown some value it wouln't be selected. Any advices/suggestions?
UPDATE I'd mention that I don't see at XHR console call of floorlist. How can I make it?
UPDATE

$(document).ready(function()
{
    $('#OpenLessons_Building').trigger('change');
    $('#OpenLessons_Building option').trigger('click');
}).on('change','#OpenLessons_Building',function()
{
    console.log('changed');
}).on('click','#OpenLessons_Building option',function()
{
    console.log('clicked');
});

Tried like this. Both actions - change and click has happened because I can see output in console, but the dependent arrays are still empty.

回答1:

You should use $("#OpenLessions_Building").on("change", function() { your code here });

The trigger function was actually does is execute the action (change or click in your case) of the dropdown as soon as the page loads.