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.

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

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端