在下拉字段中选择2远程源

时间:2013-11-10 19:01:44

标签: php jquery ajax jquery-select2 inline-editing

我有一个城市字段,在内联调用它将在下拉列表中加载ajax数据。请检查我的代码和&让我知道我错在哪里。我看了select2 documentation

<script type="text/javascript">
    jQuery(function($) {
        $('#city_id').editable({
            type: 'select2',
            name: 'otmp_tx_user_details:city_id',
            pk:"userdetailid:<?php if($student_info->userdetailid) echo $student_info->userdetailid; else echo "0";?>",
            ajax: {
                url: "<?php echo site_url()?>students/get_city_by_country", 
                dataType: 'json',
                data: function () {
                    return;
                },
                results: function (data) {
                    return {results: data};
                }
            },
            url: "<?php echo site_url();?>students/inlineedit",
            success: function(data) {
            }
        });
    }); 
</script>

这是我在PHP文件中的ajax数据:

$array = array(
               array("id"=>1,text=>"Dhaka"), 
               array("id"=>2,text=>"Pabna")
);
echo json_encode($array);

请帮我解决问题。

1 个答案:

答案 0 :(得分:1)

尝试将Ajax对象包装在select2对象中,如下所示:

jQuery(function($) {
    $('#city_id').editable({
        type: 'select2',
        name: 'otmp_tx_user_details:city_id',
        pk:"userdetailid:<?php if($student_info->userdetailid) echo $student_info->userdetailid; else echo "0";?>",
        select2: {
            ajax: {
                url: "<?php echo site_url()?>students/get_city_by_country", 
                dataType: 'json',
                data: function () {
                    return;
                },
                results: function (data) {
                    return {results: data};
                }
            }
        },
        url: "<?php echo site_url();?>students/inlineedit",
        success: function(data) {
        }
    });
});