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 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号