Json结果只返回1行

时间:2014-06-17 13:32:36

标签: jquery json

由于某种原因,我的函数只从我的json数据中吐出1行。我怎么能做这个循环?我确定我错过了一些简单的东西!

JSON

[
    {"id":"3","job_title":"Aircraft Technician","job_description":"","job_qualifications":null,"created_at":"2014-06-02 18:00:32","updated_at":"2014-06-02 18:00:32","industries":"","division_id":"3","industry_job_id":"3"},
    {"id":"4","job_title":"Aircraft Technician 2","job_description":"","job_qualifications":null,"created_at":"2014-06-02 18:18:57","updated_at":"2014-06-02 18:18:57","industries":"","division_id":"3","industry_job_id":"4"}
]

JQUERY

function getIndustryJobs(data_id, data_option){       
    // alert("data-id:"+data_id+", data-option:"+data_option);
    $.getJSON("{{{ Config::get('app.url') }}}/data/json/"+data_id, function(data) {
        $('#roleList').empty();                                                              
        $.each(data, function(key, val){              
            $('#roleList').append('<option value=' + val.id + '>' + val.job_title + '</option>');
            $('#roleList').listview('refresh');
        });

    });
}

HTML

  <select id="roleList" name="roleList" multiple="multiple"></select> 

2 个答案:

答案 0 :(得分:3)

很可能,这是因为您正在请求一行,我可以从网址中猜到:.../data/json/"+data_id。这意味着如果您请求所有行,您的代码应该可以正常工作。其余的代码看起来很好,除了refresh,它应该在循环之外,因为@AnoopJoshi正确地说:

function getIndustryJobs(data_option){       
    $.getJSON("{{{ Config::get('app.url') }}}/data/json", function(data) {
        $('#roleList').empty();                                                              
        $.each(data, function(key, val){              
            $('#roleList').append('<option value=' + val.id + '>' + val.job_title + '</option>');
        });
        $('#roleList').listview('refresh');

    });
}

答案 1 :(得分:0)

请尝试这个,这是工作,我累了

    <script>
        $(document).ready(function () {
            var data = JSON.parse('[{"id":"3","job_title":"Aircraft Technician","job_description":"","job_qualifications":null,"created_at":"2014-06-02 18:00:32","updated_at":"2014-06-02 18:00:32","industries":"","division_id":"3","industry_job_id":"3"},{"id":"4","job_title":"Aircraft Technician 2","job_description":"","job_qualifications":null,"created_at":"2014-06-02 18:18:57","updated_at":"2014-06-02 18:18:57","industries":"","division_id":"3","industry_job_id":"4"}]');
            $(data).each(function () {
                $('#roleList').append('<option>' + this.id + this.job_title + '</option>');
            });
        });
    </script>