0

我在 yii 中遇到了问题,我创建了代码来调用 activedropDownList 中的 ajax 来加载 dropDownList 中的值,并且一切正常!

但我的问题是:

1-添加时 array('prompt'=>'Select Yous country ') 所以代码将是这样的:

echo CHtml::activedropDownList($model,'country',$model->getcountry(),array('prompt'=>'Select Yous country'),

结果:不行。

但是,如果当我删除 array('prompt'=>'Select Yous country') 时,它的工作和 ajax 加载新列表!

这是我的代码

看法 :

<?php
/* @var $this UsersController */
/* @var $model Users */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'users-index-form',
    'enableAjaxValidation'=>false,
)); ?>







    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'الأسم الأول'); ?>
        <?php echo $form->textField($model,'first_name'); ?>
        <?php echo $form->error($model,'الأسم الاول'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'العائلة'); ?>
        <?php echo $form->textField($model,'last_name'); ?>
        <?php echo $form->error($model,'العائلة'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'كلمة المرور'); ?>
        <?php echo $form->passwordField($model,'password'); ?>
        <?php echo $form->error($model,'password'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'تأكيد كلمة المرور'); ?>
        <?php echo $form->passwordField($model,'passwordconfirm'); ?>
        <?php echo $form->error($model,'passwordconfirm'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'البريد الإلكتروني '); ?>
        <?php echo $form->textField($model,'email'); ?>
        <?php echo $form->error($model,'email'); ?>
    </div>
        <div class="row">
        <?php echo $form->labelEx($model,'تأكيد البريد الإلكتروني'); ?>
        <?php echo $form->textField($model,'emailconfirm'); ?>
        <?php echo $form->error($model,'emailconfirm'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'تاريخ الميلاد'); ?>
    <?php

$this->widget(
    'ext.jui.EJuiDateTimePicker',
    array(
        'model'     => $model,
        'attribute' => 'birth_date',
        'language'=> 'en',//default Yii::app()->language
         'mode'    => 'date',//'datetime' or 'time' ('datetime' default)
        'options'   => array(
        'dateFormat' => 'yy-mm-dd',
            //'timeFormat' => '',//'hh:mm tt' default
        ),
    )
);


?>

        <?php echo $form->error($model,'birth_date'); ?>
    </div>





    <div class="row">
        <?php echo $form->labelEx($model,'الجنس'); ?>
        <?php echo chtml::activeDropDownList($model,'gender',$model->getStatusOption(),array('prompt'=>'أختر جنسك')
); ?>
        <?php echo $form->error($model,'gender'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'البلد'); ?>
    <?php

    /*
     echo chtml::activeDropDownList($model,'country',$model->getcountry(),array('prompt'=>'Select your country  ')   , 
    array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('current/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city', //selector to update
//'data'=>'js:javascript statement' 
//leave out the data key to pass all form values through
))

    ); 

    */
    echo CHtml::activedropDownList($model,'country',$model->getcountry(),array('prompt'=>'اختر البلد أو المنطقة '),

    array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('register/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city', //selector to update
//'data'=>'js:javascript statement' 
//leave out the data key to pass all form values through
))
    );


   ///




    ////

    ?>


        <?php echo $form->error($model,'country'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'المدينة'); ?>
        <?php 
       //empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city','', array());

//echo CHtml::activedropDownList($model,'city',array());
 ?>
        <?php echo $form->error($model,'city'); ?>
    </div>



    <div class="row">
        <?php echo $form->labelEx($model,'رمز الاتصال'); ?>
        <?php echo $form->textField($model,'mobile_code'); ?>
        <?php echo $form->error($model,'mobile_code'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'رقم الجوال'); ?>
        <?php echo $form->textField($model,'mobile_number'); ?>
        <?php echo $form->error($model,'mobile_number'); ?>
    </div>




    <div class="row buttons">
        <?php echo CHtml::submitButton('تسجيــــــــــــل'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

Ajax 控制器:

public function actionDynamiccities() /// Called Ajax
{
$country=intval($_POST['Users']['country']);

       $data=Cities::model()->findAll('country_id=:country_id', 
                  array(':country_id'=>$country));
 $data=CHtml::listData($data,'id','city_name_e');

    foreach($data as $value=>$name)
    {
        echo CHtml::tag('option',
                   array('value'=>$value),CHtml::encode($name),true);
    };


}  

提前致谢

4

1 回答 1

2

activeDropDownList只需要 4 个参数。

公共静态字符串 activeDropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ())

放入'prompt'=>'اختر البلد أو المنطقة '与您的 ajax 代码相同的数组中。

array(
  'prompt'=>'اختر البلد أو المنطقة ',
  'ajax' => array(
    'type'=>'POST', //request type
    'url'=>CController::createUrl('register/dynamiccities'), //url to call. //Style: CController::createUrl('currentController/methodToCall')
    'update'=>'#city', //selector to update
    //'data'=>'js:javascript statement' 
    //leave out the data key to pass all form values through
  )
)
于 2012-12-05T16:26:59.017 回答