我有2个dropDownList第一个下拉列表是小时第二个下拉列表的分钟数取决于小时
示例如果选择了"小时"框,则"8分钟"框具有30-60如果选择"小时"框,则"13分钟"框具有00-60
我使用了ajax请求,但没有工作。
代码显示在下方
index.php
<?php
$this->breadcrumbs=array(
'Book',
);
?>
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'test',
'type'=>'horizontal',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<div class="row">
<?php
echo $form->dropDownList( $model, 'C_time',$this->HH(), array( 'empty' => 'Select Hour ',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('CustomersController/MM'),
'update'=>'#Minute',
'data'=>array('hour' => 'js:this.value'),
),
)
);
echo CHtml::dropDownList('Minute','', array('empty'=>'Select Minute'));
?>
</div>
<?php $this->endWidget(); ?>
客户控制器
public function actionIndex() {
$model = new Customers();
$this->render('index',array('model'=>$model));
}
public function HH()
{
$r_model = new Restaurant();
$par1 = 'HOUR';
$par2 = 'R_open';
$open = $r_model->getTime($par1,$par2);
$par2 = 'R_close';
$close = $r_model->getTime($par1,$par2);
$rs = array();
for ($i = $open; $i < $close; $i++){
$rs[$i] = $i;
}
return $rs;
}
public function actionMM()
{
if(isset($_POST['hour']) && $_POST['hour']!=''){
$this->render('test');
$hour=$_POST['hour'];
$r_model = new Restaurant();
$start = '00';
$end = '60';
if($hour == $r_model->getTime('HOUR','R_open')){
$par1 = 'MINUTE';
$par2 = 'R_open';
$open = $r_model->getTime($par1,$par2);
$start = $open;
}
elseif($hour == $r_model->getTime('HOUR','R_close')){
$par1 = 'MINUTE';
$par2 = 'R_close';
$close = $r_model->getTime($par1,$par2);
$end = $close;
}
$rs = array();
for ($i = $start; $i < $end; $i++){
$rs[$i] = $i;
}
echo CHtml::tag('option',array('value' => ''),
CHtml::encode('Select User Type'),true);
foreach($rs as $id)
{
echo CHtml::tag('option',
array('value' => $id),CHtml::encode($id),true);
}
}
}
您不需要ajax!
第一个初始的第一个下拉列表包含所有选项,然后第二个下拉列表(分钟)包含空选项,
然后根据第一个下拉列表,填写第二个,如:
<div class="row">
<?php
echo $form->dropDownList(
$model, 'C_time',$this->HH(), array( 'empty' => 'Select Hour ',
'id' => 'drpHour', // give it an id so we can select it
)
);
echo CHtml::dropDownList('Minute','', $data , array('empty'=>'Select Minute', 'id' => 'drpMinute'));
?>
</div>
<script>
$(document).ready(function(){
$('#drpHour').change(function(){ // depending on your logic, fill the second drop down
if($('#drpHour').val() > 13){
$('#drpMinute').append('<option value="30" >30</option>');// and fill the rest
...
}
else{ ... }
});
});