2011-04-14 18:14:01 0 评论 Yii 1.0 Boy.Lee

Yii 1.0框架使用 Ajax

{Controller}

//Ajax Upgrade
public function actionACountry(){
    $this->render('ac');
}

public function actionAcity(){
    $arrCity = array(
        '1'=>array('1'=>'NY', '2'=>'Bosston', '3'=>'DC'),
        '2'=>array('1'=>'Paris', '2'=>'Versailles', '3'=>'Nice'),
        '3'=>array('1'=>'Tokyo', '2'=>'Osaka', '3'=>'Nagoya'),
        );

    @$intCid = (int)$_POST['country_id'];

    if (!empty($intCid)){
        foreach ($arrCity[$intCid] as $key=>$cityName){
            echo CHtml::tag('option',array('value'=>$key),CHtml::encode($cityName),true);
        }
    }

    echo '<pre>';
    print_r($_POST);
    print_r($arrCity[$intCid]);
    echo '</pre>'; 
}


{View : ac.php}

echo CHtml::beginForm();
    echo CHtml::dropDownList(
            'country_id',
            '',
              array('0'=>'Choice One',
                        '1'=>'USA',
                        '2'=>'France',
                    '3'=>'Japan',),
              array(
                  'ajax'=>array(
                      'type'=>'POST',
                      'url'=>Yii::app()->createUrl('lab/acity'),
                      'update'=>'#city_id',
    //                              'data'=>'js:javascript statement',
                  )));
    echo CHtml::dropDownList('city_id', '', array('Country First'));

    echo '<div>'.CHtml::textField('TSource','',array('ajax'=>array(
      'type'=>'POST',
      'url'=>'acity',
      'update'=>'#TOut',
      'data'=>'js:jQuery("#country_id").serialize()',
    )));
    echo CHtml::ajaxButton(
     'Ajaxing....^&^',
      Yii::app()->createUrl('lab/acity'),
      array('type'=>'POST',
                'update'=>'#TOut',
                'data'=>'js:jQuery("#country_id").serialize()',
            'beforeSend'=>'function(){$("#btn_BoyLee").hide();$("#TOut").show()}',
            'success'=>'function(html){ alert(html) }',
            'complete'=>'function(){window.location.reload()}',
            'error'=>'function(a,b,c){alert(b)}'
      ),
      array('id'=>'btn_BoyLee')
      );
    echo CHtml::textArea('TOut','',array('cols'=>'120','rows'=>'30','style'=>'display:none'));
echo CHtml::endForm();


{Boy Say}

这是一个高级版本, 相关内容应该都可以在这里找到.