首页 > 解决方案 > 从 json 中获取值并放入下拉列表

问题描述

表单.php

数组(gettext('网关名称'),数组(“名称”=>“网关ID”,“类”=>“网关ID”,“ID”=>“网关ID”),“选择”,“”,“修剪| required|xss_clean', 'tOOL TIP', '请先选择网关', 'id', 'name', 'gateways', 'build_dropdown', 'where_arr', array( "status" => "0" ) ),

视图.php

$(".gateway_routing_type").change(function(){ var gateway_routing_type =$('.gateway_routing_type option:selected').val();

        $.ajax({
            type:'POST',
            url: "<?= base_url() ?>/trunk/trunk_sipdevicelist/",
            data:"gateway_routing_type="+gateway_routing_type, 
            success: function(response) { 
                var tmp = jQuery.parseJSON(response); 
               
                console.log(response);
                $("#gateway_id").empty(); 
         
                 $.each(tmp, function(key, value) { 
                    
        $("#gateway_id").append($('<option></option>').val(value.id).html(value.name));
                 });
                
                    $(".selectpicker").selectpicker('refresh');
           
          
            }
        });
   

});

控制器.php

$add_array = $this->input->post(); $gateway_routing_type = $add_array['gateway_routing_type'];

    $accountinfo = $this->session->userdata("accountinfo");
    $gateway_array = array();
    if($gateway_routing_type == "0"){
        $gateway_result = $this->db->get_where('gateways', array(
            "gateway_routing_type" => $gateway_routing_type,
            "status" => 0
        ));
        if ($gateway_result->num_rows() > 0) {
            $gateway_result = $gateway_result->result_array();
            foreach ($gateway_result as $key => $value) {
                $gateway_array[] = array(
                    "id"=>$value['id'],
                    "name" => $value['name']
                );
            }
            
        } 
    }else{
        $this->db->select('GROUP_CONCAT(id) as provider_id');
        $provider_id = (array)$this->db->get_where("accounts",array( "status"=>0,"deleted"=>0,"type"=>3 ))->first_row();
        
        $array = explode("," ,$provider_id['provider_id']);
        
        foreach($array as $value){
            $sipdevice_result = $this->db->get_where("sip_devices",array("accountid"=>$value));
            if ($sipdevice_result->num_rows() > 0) {
                $sipdevice_result = $sipdevice_result->result_array();
                foreach ($sipdevice_result as $key => $value) {
                    $gateway_array []= array(
                        "id"=>$value['id'],
                        "name" => $value['username']
                    );
                }                   
            } 
        }
           
    }

当ajax调用它发生然后它开始运行然后它显示选定的下拉值

标签: jqueryajaxcodeigniter

解决方案


推荐阅读