0

有 4 个像魅力一样工作的下拉列表,除非我像这样从控制器传递了预选值

public function actionBodyTrim(){
    $trim = new CarTrim;
    $spares = new Spares;

    $spares->makeid  = $_REQUEST['mid'];
    $spares->modelid = $_REQUEST['moid'];
    $spares->bodyid  = $_REQUEST['bid'];
    $spares->trimid  = $_REQUEST['tid'];

    $this->render('bodytrim', array(
                  'trim'=>$trim,
                  'spares'=>$spares
                  )
    );
}

呈现页面时,第一个下拉列表显示预选值。我已经使用下面的代码测试了第二个下拉列表,但它不会触发 Ajax 调用。结果如下所示

<div class="span5 leftpull">
    <div class="row">
     <div class="width125"><?php echo $form->labelEx($model,'makeid'); ?></div>
     <div class="widthtxt"><?php echo $form->dropDownList($model,'makeid', CHtml::listData(Makes::model()->findAll(array('order' => 'makename ASC')), 'makeid', 'makename'),
                                     array('prompt'=>'Select Makes',
                                           'ajax'=>array('url'=>CController::createUrl('CarModels'),
                                            'beforeSend' => 'function(){$("#carmodels").addClass("loading");}',
                                            'complete' => 'function(){$("#carmodels").removeClass("loading");}',
                                           'type' =>'POST',
                                           'update'=>'#'. CHtml::activeId($model, 'modelid'),
                                           array('class'=>'ajaxlink'),

                                        ))); ?>


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

<div class="row" id="carmodels">
     <div class="width125"><?php echo $form->labelEx($model,'modelid'); ?></div>
     <div class="widthtxt"><?php echo $form->dropDownList($model, 'modelid', empty($model->modelid) ? array('prompt' => 'Select the country first') :
                                      array('ajax'=>array('type'=>'POST',
                                                          'url'=>CController::createUrl('BodyType'),
                                                          'update'=>'#'. CHtml::activeId($model, 'bodyid'),
                                                          'prompt'=>'Models',
                                                          array('class'=>'ajaxlink'),

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

Ajax 的 HMTL 代码未按要求触发。

 <div class="row" id="carmodels">
         <div class="width125"><label for="Spares_modelid" class="required">Model <span class="required">*</span></label></div>
         <div class="widthtxt"><select name="Spares[modelid]" id="Spares_modelid">
<optgroup label="ajax">
<option value="type">POST</option>
<option value="url">/spareparts/index.php?r=spares/parts/BodyType</option>
<option value="update">#Spares_bodyid</option>
<option value="prompt">Models</option>
<optgroup label="0">
<option value="class">ajaxlink</option>
</optgroup>
</optgroup>
</select>                   </div>
    </div>

我在哪里弄错了?

4

2 回答 2

0

看起来您可能在bodyid表单字段中混淆了一些参数。如果您查看生成的 HTML,您的 ajax 请求将不正确地呈现。

<optgroup label="ajax">
<option value="type">POST</option>
<option value="url">/spareparts/index.php?r=spares/parts/BodyType</option>
<option value="update">#Spares_bodyid</option>
<option value="prompt">Models</option>
<optgroup label="0">
<option value="class">ajaxlink</option>
</optgroup>
</optgroup>   
于 2013-10-13T06:53:31.450 回答
0

我已经设法使用(if 语句)使其与肮脏的工作轮一起工作。我只是希望有人有比这更干净的代码。

 <?php
             if (isset($model->makeid) && !isset($model->modelid)  ) 
             {              
                            $list = CHtml::listData(CarModels::model()->findAll('makeid=:mid',
                                                                                                                array(':mid'=>$model->makeid)), 'modelid', 'model');
                            echo $form->dropDownList($model,'modelid', $list, 
                                                                          array( 'prompt'=>'Select Models',
                                                                                             'ajax'=>array('type'=>'POST',
                                                                                                                      'url'=>CController::createUrl('BodyType'),
                                                                                                                      'update'=>'#'. CHtml::activeId($model, 'bodyid'),

                                             ))); 

             }

             else (isset($model->makeid) && isset($model->modelid) ){
                      $list = CHtml::listData(CarModels::model()->findAll('makeid=:mid  AND  modelid=:mod',
                                                                                                                array(':mid'=>$model->makeid, ':mod'=>$model->modelid )), 'modelid', 'model');

                    echo $form->dropDownList($model,'modelid', $list, array('prompt'=>'Select Models'));

              }

?>

于 2013-10-14T11:31:29.007 回答