0

我很难做到这一点,我正在使用 codeigniter php 框架,基本上我有 3 个选择菜单,它的值来自数据库,例如:

<?php

$this->data[selOne] = $this->model->selectOne();      //the id of this one must send
$this->data[selTwo] = $this->model->selectTwo();     //<-to here and the id of 2nd select 
$this->data[selThree] = $this->model->selectThree(); //must send to here <-

$this->load->view->('controller/index.php', $this->data);
?>



在我看来

 <select>
    <?php foreach($selOne as $one){?>
    <option><?php echo $one->label;?></option>
    <?php }?>
 </select>

 <select>
    <?php foreach($selTwo as $one){?>
    <option><?php echo $one->label;?></option>
    <?php }?>
 </select>

 <select>
    <?php foreach($selThree as $one){?>
    <option><?php echo $one->label;?></option>
    <?php }?>
 </select>

我想实现的是
*当我选择第一个选择菜单时它将其值传递给第二个菜单而不刷新页面。
* 与第二个选择菜单相同,它将其值传递给第三个选择菜单。

例如

在第一个选择菜单中,我选择“亚洲”

第二个选择菜单获取值亚洲,因此它显示亚洲的国家,我也单击其国家之一

第三个选择它返回在第二个选择菜单中选择的国家的城市

sql部分没问题我只需要知道如何在不刷新页面的情况下传递它的值。

谁能帮我实现这一点,请放过我。

4

2 回答 2

3

//add class on first contents_name in html

//your custom_jqueryfile.js

$(document).ready(function() {
    $("body").delegate('.contents_name','change',function () {
        var continent_id = $(this).val();
        $.ajax( {
            type:GET,
            data: ({"continent_id":continent_id}),
            url: 'mysite.com/myajax',
            success: function (data_response) {
                $(".countries_target").html(data_response);
            },
            error:function () {
                alert('something went wrong!');
            }
        })
    } )
})

//CREATE NEW CI CONTOLLER ///learn about CI-URI

class myajax extends CI_Controller {
    public function __construct() {
        parent::__contruct();
    }

    public function index() {
        $check_uri_params = $this->uri->rsegment_array();
        //check where content_id showing print_r($check_uri_params) below URI_SEGMENTS to check in which params $content_id     

        ///$content_id = $this->uri->segment(3, 0); ---SOMETHING LIKE LEFT CODE TO GET CONTENT_ID
        $data['country_names'] = $this->db->get_where('country_name_table', array('id' => $content_id));
        $this->load->view('ajax_options',$data);            
    }
}

//Create views named it ajax_options.php

echo '<select name="country_name">';
foreach($country_names as $country_name) {
        echo '<option value="'.$country_name['countryID_col_name'].'">'.$country_name['countryName_col_name'].'</option>';
}
echo '</select>';
于 2013-09-09T11:08:19.100 回答
1

像这个控制器一样做:

function get_zone_menu()
{
    $id = $this->input->post('id');
    $zones  = $this->Location_model->get_zones_menu($id);

    foreach($zones as $id=>$z):?>

    <option value="<?php echo $id;?>"><?php echo $z;?></option>

    <?php endforeach;
}

javascript:

$(function(){
$('#f_country_id').change(function(){
        $.post('<?php echo site_url('locations/get_zone_menu');?>',{id:$('#f_country_id').val()}, function(data) {
          $('#f_zone_id').html(data);
        });
    });

});

于 2013-09-09T11:26:31.523 回答