如何在codeigniter中隐藏form_dropdown

时间:2016-05-19 03:35:12

标签: php ajax codeigniter

我希望在第二次选择后隐藏表单下拉,因为它已经在之前选择了第二个组合框。目前我是这样的。

$data['year'] = NULL;

但我需要的是在选择下拉列表1后变量消失。

enter image description here

以下是我的代码。

控制器

function carbyyear() {
    $arrCaryear = $this->modelRegister->loadcaryear();

    $arrcaryear[''] = 'Please Select';
    foreach ($arrCaryear as $caryears) {
        $arrcaryear[$caryears->year] = $caryears->year;
    }

    $data['year'] = $arrcaryear;

    $this->load->view('abc',$data); 
}

function ajax_car_make() {

   if (isset($_POST) && isset($_POST['year'])) {

        $year = $_POST['year'];
        $arrMakes = $this->modelRegister->loadcarmake($year);

        //print_r($arrModels);
        $arrmakes[''] = 'Please Select';
        foreach ($arrMakes as $makes) {
            $arrmakes[$makes->make] = $makes->make;
        }

        print form_dropdown('make',$arrmakes);

        $data['year'] = NULL;

        $this->load->view('abc',$data); 

        } else {
        redirect('site');
    }   
}

function ajax_car_model() {

   if (isset($_POST) && isset($_POST['make'])) {

        $make = $_POST['make'];
        $arrModels = $this->modelRegister->loadmodelfrombrand($make);

        //print_r($arrModels);
        $arrmodels[''] = 'Please Select';
        foreach ($arrModels as $models) {
            $arrmodels[$models->model] = $models->model;
        }

        print form_dropdown('model',$arrmodels);
    } else {
        redirect('site');
    }   
}

查看

<body>
    <div id="container">
        <div id="body">
            <article>
                <table style="margin:0 auto;width:50%" >
                    <tr>
                        <td align="center" height="50">
                            <div id="yearcombox">
                            <?php 

                                    $js = 'id="year" onChange="showMake(this);;"';
                                    echo form_dropdown('year',$year); 

                            ?>
                            </div>

                        </td>
                    </tr>
                    <tr>
                        <td align="center" height="50"><div id="makecombox"></div> </td>
                    </tr>
                    <tr>
                        <td align="center" height="50"><div id="modelcombox"></div> </td>
                    </tr>
                </table> 
             </article>
        </div>
    </div>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
                 $(document).ready(function () { 
                    $('#yearcombox select').change(function () {
                        var selYear = $(this).val();
                        console.log(selYear);
                        $.ajax({   
                            url: "ajax_car_make", 
                            async: false,
                            type: "POST", 
                            data: "year="+selYear, 
                            dataType: "html", 
                            beforeSend: function(data) {
                                        $('#makecombox').html('<img src="<?php echo base_url(); ?>/template/images/loader.gif" alt="" width="24" height="24">');
                                    },                        
                            success: function(data) {

                                $('#makecombox').html(data);

                            },
                        })
                    });
                });

                $(document).ready(function () { 
                    $('#makecombox select').change(function () {
                        var selMake = $(this).val();
                        console.log(selMake);
                        $.ajax({   
                            url: "ajax_car_model", 
                            async: false,
                            type: "POST", 
                            data: "make="+selMake, 
                            dataType: "html", 
                            beforeSend: function(data) {
                                        $('#modelcombox').html('<img src="<?php echo base_url(); ?>/template/images/loader.gif" alt="" width="24" height="24">');
                                    },                        
                            success: function(data) {

                                $('#modelcombox').html(data);

                            },
                        })
                    });
                });



    </script>
</body>

1 个答案:

答案 0 :(得分:2)

无需在ajax响应中加载视图

function ajax_car_make()
{
   if (isset($_POST) && isset($_POST['year'])) {
    $year = $_POST['year'];
    $arrMakes = $this->modelRegister->loadcarmake($year);
    //print_r($arrModels);
    $arrmakes[''] = 'Please Select';
    foreach ($arrMakes as $makes) {
        $arrmakes[$makes->make] = $makes->make;
    }
    echo form_dropdown('make',$arrmakes);

    } else {
    echo false;
}   

}

更改$('#makecombox select').change(function () {

$('body').on('change','#makecombox select',function () {