下拉人口使用jquery从codeigniter控制器获取数据 [英] Dropdown population using jquery to get data from codeigniter controller

查看:135
本文介绍了下拉人口使用jquery从codeigniter控制器获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jquery和ajax填充下拉列表。



我的jquery调用是

 < script> 
$(document).ready(function(){
$ .ajax({
type:GET,
url:<?= base_url ; user / getstate /,//调用获取数据的脚本
data:'',//可以插入url argumnets在这里传递给api.php
dataType:'json' / data format
success:function(data){//接收回复
$(#state)append('< option selected> State< / option>');
for(i in data)
$(#state)。append(< option value = \+ data [i] [0] +\>+ data [i] [1] +< / option>);
}

});
< / script>

需要填充的部分为

 < select id =stateclass =span2style =height:30pxname =staterequired> 
< / select>

控制器中的函数是

  public function getstate(){

$ this-> load-> model('usermodel');
$ data = $ this-> usermodel-> getstate();
echo json_encode($ data);


}

获取的是

  public function getstate(){
$ sql =SELECT * FROM statelist;
$ query = $ this-> db-> query($ sql);
$ result = $ query-> result_array();
return $ result;
}

在statelist中的数据是



sid | statename



wb |西孟加拉邦



ga | Goa



我面临的问题是,当我加载页面时,我得到下面的2个元素,如预期,但在这种形式





$
未定义


未定义



不明白为什么我得到未定义而不是所需或预期的数据,如



状态


西孟加拉邦$ b $b¨
Goa



一个解决方案会真正帮助我。

解决方案

Jquery将你的json变成一个对象,所以你需要引用它:

  data .i 

  data [i] 



但我猜,我不是你的关键。如果您在成功参数中尝试并记录数据console.log(data),您应该能够看到正确的键名称。


I want to populate a dropdown using jquery and ajax.

my jquery call is

<script>
        $(document).ready(function(){
            $.ajax({           
                type: "GET",
                url: "<?=base_url()?>user/getstate/",                  //the script to call to get data          
                data:'',                        //you can insert url argumnets here to pass to api.php
                dataType: 'json',                //data format      
                success: function(data){    //on recieve of reply
                    $("#state").append('<option selected>State</option>');
                    for(i in data) 
                        $("#state").append("<option value=\""+data[i][0]+"\">"+data[i][1]+"</option>");
                } 

            });
        });
    </script>

the part that needs to be populated is

<select id="state" class="span2" style="height:30px" name="state" required>
</select>

the function in the controller is

public function getstate(){

        $this->load->model('usermodel');
        $data=$this->usermodel->getstate();
        echo json_encode($data);


}

the model from which the data is being fetched is

public function getstate(){
    $sql="SELECT * FROM statelist";
    $query=$this->db->query($sql);
    $result=$query->result_array();
    return $result;
}

the datas in statelist are

sid | statename

wb | West Bengal

ga | Goa

The problem i am facing is that when I load the page I get the dropdown with 2 elements as expected but in this form

State
undefined
undefined

Now i don't understand why am i getting "undefined" instead of the required or expected data like

State
West Bengal
Goa

A solution will really help me.

解决方案

Jquery turns your json into an object, so you need to referene it like:

data.i

not

data[i]

like you would with an array.

But im guessing that i is not your key. If you try and log data "console.log(data)" in success parameter, you should be able to see the correct key names.

这篇关于下拉人口使用jquery从codeigniter控制器获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆