以下 jquery ajax 代码适用于 #MerryParentEmail keyup 事件,但不适用于 #MerryParentState 更改事件。更改事件正在触发,但城市下拉框未填充。
<script type="text/javascript">
$(document).ready(function(){
$("#MerryParentStateId").change(function(){
state=$(this).val();
txt_str="state_id="+state;
$.get("../students/getcities",txt_str,function(result){
$("#MerryParentCityId").html(result);
});
});
});
</script>
student_controller.php
function getcities(){
$options = $this->Student->MerryParent->City->getCities($this->data['MerryParent']['state_id']);
print_r($options);
foreach ($options as $k=>$v){
echo '<option value="'.$k.'">'.$v.'</option>';
}
}
添加.ctp
<?php
echo $this->Session->flash();
echo $this->Form->create('Student');
echo '<fieldset>';
echo '<legend>Student Information</legend>';
echo $this->Form->input('Student.name');
$options = array('Male'=>'Male','Female'=>'Female');
$attributes = array('value'=>'Male');
echo $this->Form->radio('Student.gender',$options,$attributes);
echo $this->Form->input('Student.dob', array('label'=>'Date of Birth',
'dateFormat'=>'DMY',
'empty'=>'Choose one',
'timeFormat' => '',
'minYear' => (
date('Y') - 5
),
'maxYear' => (
date('Y') - 2
)
));
echo $this->Form->input('Student.merry_class_id',
array(
'label'=>'Enquiry Class for',
'empty'=>'Choose one',
'options'=>array('1'=>'Playgroup','2'=>'Nursery','3'=>'LKG', '4'=>'UKG')
)
);
echo '</fieldset>';
echo '<fieldset>';
echo '<legend>Parent Information</legend>';
//echo $form->input('Student.parent_id', array('type'=>'hidden'));
echo $this->Form->input('MerryParent.initial',
array('empty'=>'Choose one',
'options'=>array('Dr'=>'Dr',
'Mr'=>'Mr',
'Mrs'=>'Mrs',
'Ms'=>'Ms')
)
);
echo $this->Form->input('MerryParent.email');
echo $this->Form->input('MerryParent.name', array('label'=>'Parent/Guardian Name'));
echo $this->Form->input('MerryParent.landline');
echo $this->Form->input('MerryParent.mobile');
echo $this->Form->input('MerryParent.address');
echo $this->Form->input('MerryParent.state_id', array('empty'=>'Choose one','options'=>$states));
echo $this->Form->input('MerryParent.city_id');
echo $this->Form->input('MerryParent.postal_code');
echo '</fieldset>';
echo $this->Form->end('Submit');
?>