dongtang5229 2014-01-21 21:56
浏览 16
已采纳

Yii下拉列表取决于两个下拉列表

I have seen this wiki where they populate a dropdownlist of cities, depending of the value of another dropdownlists that contains countries, using an ajax call with the update option. I need to implement something similar, but my dropdownlist depends on two dropdownlists:

<div class="row">
    <?php echo CHtml::label('Countries', 'country_id'); ?>
    <?php echo CHtml::dropdownlist('country_id', '',$countries); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'globaladmin'); ?>
    <?php echo $form->dropDownList($model,'globaladmin',User::itemAlias('AdminStatus')); ?>
    <?php echo $form->error($model,'globaladmin'); ?>
</div>

The user must select a country, and then only if in the second list "No" is selected, a new dropdown lists must be populated with the cities info (just like in the wiki example).

As I said it is similar to the example, but the new dropdown depends on 2 values (the id of the contry selected on the first list and if "No" is selected in the second one). How could I solve it?

EDIT: Explaining a little more

In the example, the country dropdown which contains the ajax call is like:

echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //selector to update
))); 

I can't define such a ajax call in my country list, because I must wait the value of the second dropbox. Only if "No" is selected in this list, the ajax will be executed (and the city dropdownlist populated and showed). If "Yes" is selected then the city dropdownlist must be hidden.

  • 写回答

1条回答 默认 最新

  • doubeng1278 2014-01-21 22:17
    关注

    Just add an ID to your globaladmin drop down and then do like below:

    $("#THE ID YOU HAVE SET").on('change',function(){
        // You can use $(this).val()
        // and send it via an ajax request into your own url
        var valueOfMyGlobalAdminDropDown=$(this).val();
        if(valueOfMyGlobalAdminDropDown=="yes"){
           //DO SOMETHING
        }else{
           //DO SOMETHING ELSE
        }
    });
    

    it is also strongly recommended to use jquery live(in jquery version -1.7) or jquery ON(in jquery version +1.7) to keep your page alive in ajax requests.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像