使用ajax php codeigniter填充下拉列表

时间:2017-05-17 02:11:15

标签: php json ajax codeigniter dropdown

我一直在尝试不同论坛上提供的所有帮助,但现在放弃并在此处发布我的问题。尝试使用ajax调用填充下拉列表。以json格式成功获取数据。但是不知道填写下拉列表。代码如下: 控制器:

function getRegion()
{
    $this->load->model('Settings_model');
    $title = $this->input->get('title');

    $result =   array("region" => $this->Settings_model->getSelectedRegion($title));
    echo json_encode($result);      
}

查看:

        $(document).on('change', '#campaignSel', function() {
        changecampaign();
    });

    function changecampaign(){
        var title= "New Normal";//$('#campaignSel option:selected').text();//$('#campaignSel').text();
        $regionSel = $("#regionSel");
            $.ajax({
                type:"GET",
                url: "<?php echo base_url('Pricecomparison/getRegion'); ?>",
                data:{ "title":"New Normal"},
                datatype: "json",
                success: function(result){
                    var appenddata;
                    $.each(result, function (key, value) {
                        appenddata += "<option value = '" + value.id + " '>" + value.region + " </option>";                        
                    });
                    $('#regionSel').html(appenddata);
                },
                error: function(xhr, textStatus, error){
                    console.log(xhr.statusText);
                    console.log(textStatus);
                    console.log(error);
                }
            });
        }

回应:{&#34;地区&#34;:[{&#34; id&#34;:&#34; 1&#34;,&#34;地区&#34;:&#34; 109 FEATHERSTON ST(ELEC)\ r \ n&#34;},{&#34; id&#34;:&#34; 2&#34;,&#34;地区&#34;:&#34; ASHBURTON / MID CANTERBURY  ELEC&#34;}]} 我想用Regions填写下拉列表。

2 个答案:

答案 0 :(得分:1)

你需要循环这个区域$.each(result.region, function (key, value) {而不是结果

<强> UPDATE1:

如果条件if ($.isArray(result.region)){ ... }

,则添加

&#13;
&#13;
result =  {"region":[{"id":"1","region":"109 FEATHERSTON ST (ELEC)\r\n"},{"id":"2","region":"ASHBURTON/MID CANTERBURY ELEC"}]}
var appenddata='';
if ($.isArray(result.region)){
$.each(result.region, function (key, value) {
                        appenddata += "<option value = '" + value.id + " '>" + value.region + " </option>";    
                        
                        console.log(appenddata);
                    });
                    }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

最后,我解决了代码中的问题。我想分享下面对我的代码所做的更改: 加入:

contentType: "application/json",

parsedobj = JSON.parse(result)

所以最终的ajax代码是:

$.ajax({
                type:"GET",
                url: "<?php echo base_url('Pricecomparison/getRegion'); ?>",
                data:{ "title":title},
                contentType: "application/json",
                datatype: "json",
                success: function(result){
                     parsedobj = JSON.parse(result)
                     var appenddata='';
                        $.each(parsedobj.region, function(index, value) 
                        {
                            appenddata += "<option value = '" + index + "'>" + value.region + " </option>";    
                        });

                        $('#regionSel').html(appenddata); 
                },
                error: function(xhr, textStatus, error){
                    console.log(xhr.statusText);
                    console.log(textStatus);
                    console.log(error);
                }
            });