0

我有两个选择框,如果我选择一个选项,它将根据选项给我结果,然后结果将显示在第二个选择框中。我怎样才能做到这一点?我想,我必须做一个onchange函数,但我不知道如何从 JavaScript 中的控制器获取数据。

class StockInController extends MY_Controller {
    function index(){
        $data['main_content'] = 'stockInView';

        $this->load->model('supplierModel');
        $query = $this->supplierModel->getAllSuppliers();

        if ($query){
            $data['records1'] = $query; 
        } 

        $this->load->model('categoryModel');
        $query = $this->categoryModel->getAllCategories();

        if ($query){
            $data['records2'] = $query;
        }

        $this->load->model('itemsModel');
        $query = $this->itemsModel->getAllItems();

        if ($query){
            $data['records3'] = $query;

        }
        $this->load->view('dashboardTemplate/template',$data); 
    }

    function addStockIn(){
        //getting parameters from view 
        $data = array(
            'item_name' => $this->input->post('item_name'),
            'cat_id' => $this->input->post('cat_id')        
        );

        //$is_ajax = $this->input->post('ajax'); //or use this line
        //$this->input->is_ajax_request();

        $result = array();
        $this->load->model('stockInModel'); 
        $query = $this->stockInModel->addStocktoDB($data);

        if ($query){  //&& any other condition
            $result['res'] = 1;//process successful - replace 1 with any message
        } else {
            $result['res'] = 0;//process failed - replace 0 with any message
        }
        echo json_encode($result);//at the end of the function.
    }
}

这是视图:

<div class="control-group">
    <label for="selsear" class="control-label">Select a Category</label>
    <div class="controls">
         <select name="select1" id="seslsear" class='cho'>
            <option id = "y" value="0">--Select--</option>
            <?php foreach($records2 as $r) { ?>
            <option value="<?=$r->cat_id?>"><?=$r->cat_name?></option>
            <?php } ?>
        </select>
    </div>
</div>

<div class="control-group">
    <label for="selsear" class="control-label">Select an item</label>
    <div class="controls">
        <select name="select1" id="selsear" class='cho'>
            <option id = "y" value="0">--Select--</option>
            <?php foreach($records3 as $r) { ?>
            <option value="<?=$r->item_id?>"><?=$r->item_name?></option>
            <?php } ?>
        </select>
    </div>
</div>      
4

1 回答 1

-1

您将不得不编写一个函数来更改选择框和提要数据以成功实现 ajax

$('#selsear').change(function() {

 var form_data = {
    item_name: $('#item_name').val(),
    cat_id: $('#selsear').val()
 };

$.ajax({
       url:'controller_name/function_name',
       data:form_data,    
       datatype:'application/json',
       success:function(data){
           $.each(data,function(i){
                          htmlString="<option value='"+data[i]['item_id']+"'>'"+data[i]['item_name']+"'</option>"
           });
           $("#selsear").html(htmlString);
       }
});

控制器函数名称是从控制器检索数据的函数的名称。

我希望这能帮到您!

于 2013-01-12T13:22:37.670 回答