dongri1989 2014-07-20 11:09
浏览 36
已采纳

Yii多个下拉列表

I am newbie to yii. I am trying to create two dependent dropdownlist with one dropdownlist value. Here If I click region_id dropdownlist, it should updated cityname and cityname1 dropdownlist at the same time. I don't know how to write code for multiple ajax call. Below I have paste the code what I have tried.

<?php
echo CHtml::dropDownList('region_id','',
array(2=>'New England',1=>'Middle Atlantic',3=>'East North Central'),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loadcities'),
'data'=>array('region_id'=>'js:this.value'),
'success'=>'function(html)
{
jQuery("‪#‎city_name‬").html(html)
}' ,
array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loadcities1'),
'data'=>array('region_id'=>'js:this.value'),
'success'=>'function(html)
{
jQuery("‪#‎city_name1‬").html(html)
}' )
)));
echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select City'));
echo CHtml::dropDownList('city_name1','', array(), array('prompt'=>'Select City'));
?>

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dtot74529 2014-07-20 18:25
    关注

    You will have to write the other ajax request yourself. In fact, I prefer to store JS logic separate from view files, if it's practical (there's more than a few lines of code).

    echo CHtml::dropDownList('region_id','',
        array(2=>'New England',1=>'Middle Atlantic',3=>'East North Central'),
        array(
            'prompt'=>'Select Region',
            'ajax' => array(
                'type'=>'POST',
                'url'=>Yii::app()->createUrl('site/loadcities'),
                'data'=>array('region_id'=>'js:this.value'),
                'success'=>'function(html)
            {
                jQuery(\'#‎city_name‬\').html(html)
            }' ,
                    )));
    
    echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select City'));
    echo CHtml::dropDownList('city_name1','', array(), array('prompt'=>'Select City'));
    
    Yii::app()->clientScript->registerScript('regionChange', '
        jQuery("#region_id").change(function(){
            jQuery.ajax({
                url: "' . Yii::app()->createUrl('site/loadcities1') . '",
                type: "POST",
                data: {region_id: this.value},
                success: function(html) {
                    jQuery("#city_name1").html(html);
                }
            });
        });
    ');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥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突然无法启动