如何在yii中使用ajax下拉列表

时间:2015-04-08 09:24:44

标签: yii

如何在yii中使用ajax调用下拉...我有1 dropdown和1textfield ..如果用户从下拉列表中选择一个项目,则自动textfield必须从数据库中填充数据..任何人都可以帮助我.. < / p>

 <div class="row">
    <?php echo $form->labelEx($model,'Seattype'); ?>
	<?php echo $form->dropDownList($model,'seattype',
			  array('S' => 'Sleeper', 'M' => 'Semi-sleeper','A'=>'Seater'),
              array('empty' => '(Select Type)','name'=>'seattype'));?>
 	<?php echo $form->error($model,'seattype'); ?>
	</div>
 <div class="row">
    <?php echo $form->labelEx($model,'amount'); ?>
	<?php echo $form->textField($model,'amount',array('name'=>'amount','id'=>'amount')); ?>
	<?php echo $form->error($model,'amount'); ?>
	</div>
   
</div>
    <?php echo CHtml::submitButton('Submit',array('name'=>'submit'))?>

1 个答案:

答案 0 :(得分:0)

尝试这样的脚本,

<script>
 $('#seattype').on('change',function()
    {
            var selected_val = $(this).val();
            $.ajax({
                  type: 'POST',
                  url:   '<?php echo $this->createUrl('your_controller/your_action');?>',
                  data:  {id: selected_val},
                  success: function( amt){
                     $('#amount').val(amt);  //set returned amount value from controller
                    },
                  error: function(){
                     alert('failure');
                  }
                });

    });
</script>

带有硬编码值的样本控制器操作

public function actiongetAmount()
{
  if($_POST['id'] == 'S')
  echo 500;
  else if($_POST['id'] == 'M')
  echo 1000;
  else 
  echo 100;
}