0

次の jquery ajax コードは、#MerryParentEmail キーアップ イベントでは正常に機能しますが、#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>

students_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>';
    }

}

add.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');
 ?>
4

2 に答える 2

0

students_controller.php の getcities 関数を変更して含める場合

$this->data['MerryParent']['state_id']=$_GET['state_id'];

都市のドロップダウン ボックスに、選択した州の都市が表示されます。

以下は、変更された getcities 関数です。

function getcities(){
    $this->data['MerryParent']['state_id']=$_GET['state_id'];
    if (!empty($this->data['MerryParent']['state_id'])){
       $cities = $this->Student->MerryParent->City->getCities($this->data['MerryParent']['state_id']);
    //print_r($cities);
    foreach ($cities as $k=>$v){
        echo '<option value="'.$k.'">'.$v.'</option>';
    }

        /* foreach($cities as $optionValue){
            echo '<option>' . $optionValue . '</option>';
        }*/
    }else{
        $this->Session->setFlash('You didn\'t select a state!');
    }

}

ありがとうございました。

于 2012-04-10T03:41:07.020 に答える