【问题标题】:How to append php associative array to html using jquery ajax如何使用 jquery ajax 将 php 关联数组附加到 html
【发布时间】:2017-01-03 23:22:33
【问题描述】:

我想在我的选择框中检索这个 json 这是我的控制器代码和

public function getDistrictById()
        {
                $StateId=$this->input->post('StateId');
                $District=$this->Main_model->getDistrictListByStateId($StateId);
                //echo json_encode($District);
                foreach($District as $d)
                {

                     $rows[]=array("id"=>$d->DistrictId,"name"=>$d->DistrictName);
                }
                print json_encode($rows);

        }

这是我的 jquery/ajax 代码

<script>
function getDistrict()
 {
        var StateId=$('#state option:selected').val();
        $.ajax({
                           url:'<?php echo base_url()?>Home/getDistrictById',
                           type: 'POST',
                          dataType:"json",
                           data: {StateId:StateId},
                           success: function(response){
                            // $('#District').html(response); 
                            console.log(response);
                           },

                           error: function(){
                               alert("Fail");
                           }
                       });
 }
</script>

console.log(response);给我确切的输出,但我不知道如何将其添加到选择框中。我已经尝试过How do I add options to a DropDownList using jQuery? 和更多请帮助...

【问题讨论】:

    标签: php jquery mysql arrays


    【解决方案1】:

    在成功回调中这样做

        <script>  
        function getDistrict()
                                {
                                    var StateId = $('#state option:selected').val();
                                    $.ajax({
                                        url: '<?php echo base_url()?>Home/getDistrictById',
                                        type: 'POST',
                                        dataType: "json",
                                        data: {StateId: StateId},
                                        success: function (response) {
                                           $("#district").empty();  // clear previous options
                                            for (i = 0; i < response.length; i++)
                                            {
                                                $("#district").append("<option value='" + response[i].id + "'>" + response[i].name + "</option>");
                                            }
                                        },
                                        error: function () {
                                            alert("Fail");
                                        }
                                    });
                                }
           </script>
    

    【讨论】:

    • 不,当我添加此行时它对我不起作用,它显示错误 getDistrict 未定义。我想在选择框中填充所有相关区域
    • 抱歉,我的错误是您的回复将获得 id 和 name 属性,而不是 DistrictId 和 DistrictName。我现在更新了我的答案检查。如果仍然无法正常工作,请发布您的控制台结果
    • 我做了以下事情,但没有填充相关区域
    • 你使用select id区(小写)。在我的代码中进行相同的更改。使用#district 而不是#District
    • 是的,我已经更改了它,但它没有填充任何内容。我是 json 新手,你能告诉我 foreach($District as $d) { $rows[]=array("id"=>$d->DistrictId,"name"=>$d->DistrictName); } 打印 json_encode($rows);上面的代码给了我正确的结果。如果您对此有任何想法,我正在等待您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多