我有一个小问题,当我尝试更新(刷新)我的 div 的内容时,Ajax 会替换该 div 中的所有页面。
这里的问题: 问题的屏幕截图
这是我的控制器来源:它看起来应该如何
public function actionView() {
//print_r($_POST);
$model = null;
if( "user" == Yii::app()->user->getState("user_type")){
$model = new FormResultUser();
}
else if( "faction" == Yii::app()->user->getState("user_type")){
$model = new FormResultFaction();
}
$index = 0;
//echo $index;
if( Yii::app()->request->isAjaxRequest){
if(isset($_POST['index'])){
$index = CPropertyValue::ensureInteger( $_POST['index'] ) +1;
}
}
//echo $index;
$criteria = new CDbCriteria();
$criteria->limit = "1";
$criteria->offset = $index;
$questionModel = Question::model()->find($criteria);
// print_r($questionModel);
if (Yii::app()->request->isAjaxRequest) {
$this->render('_viewForm', array('model' => $model, 'questionModel'=> $questionModel, 'index'=>$index));
} else {
$this->render('viewForm', array('model' => $model, 'questionModel'=> $questionModel, 'index'=>$index));
}
}
而我的 viewForm: Here 你可以看到,AjaxSubmitButton 在这里。也是<div id="form">
,我想更新什么。(我也想$index
在 ajaxSubmitButton 中更新变量)
我也厌倦了' replace'
,但它不能很好地工作,问题是一样的。
<?php /* FormFilling */ ?>
<div id="form">
<?php $form=$this->beginWidget('CActiveForm'); ?>
<?php
$this->renderPartial('_viewForm', array('model' => $model, 'questionModel'=> $questionModel, 'index'=>$index, 'form'=>$form));
?>
<div class="row margin10">
<?php
echo CHtml::ajaxSubmitButton(Yii::t('strings', 'Next'), Yii::app()->createUrl('formFilling/view'), array(
'type' => 'post',
'data' => array("index" => $index),
'update' => '#form',
));
?>
</div>
<?php $this->endWidget(); ?>
</div><!-- end form -->
<?php
最后是我的_viewForm:
<?php
$answers = CHtml::listData(Answer::model()->findAll(), 'value', 'label');
?>
<div id="formAnswer" class="row"></div>
<div class="row text">
<p>
<?php echo CHtml::encode($questionModel->text); ?>
</p>
</div>
<div class="row margin10">
<span>
<?php echo CHtml::encode(Yii::t('strings', 'Answer options'), ''); ?>
</span>
<div class="margin10">
<?php
echo CHtml::activeRadioButtonList($model, 'answer_value', $answers, array("class" => "left"));
?>
</div>
</div>
<div class="row margin10">
<span>
<?php echo CHtml::label(Yii::t('strings', 'Significant topic'), 'ch_answer_important'); ?>
</span>
<?php
echo CHtml::activeCheckBox( $model,
'answer_important',
array("id" => "ch_answer_important", "class" => "left"));
?>
</div>
<?php //echo CHtml::hiddenField('index', $index) ?>