在select选项标记中循环Ajax响应

时间:2017-05-09 11:12:41

标签: php jquery json ajax codeigniter

我必须选择多个测试和日期,当根据测试点击提交时,实验室名称将加载到选择选项中

Ajax脚本

[
    {"laboratory_id":"19","laboratory_name":"ghc","laboratory_address":"cgc","laboratory_place":"jhggj","laboratory_tests":"MRI R\/L SHOULDER WITH CONTRAST"},
    {"laboratory_id":"20","laboratory_name":"BBNB","laboratory_address":"sdfds","laboratory_place":"sdfsd","laboratory_tests":"MRI R\/L SHOULDER WITH CONTRAST"},
    {"laboratory_id":"22","laboratory_name":"Anand","laboratory_address":"bsk","laboratory_place":"bengaluru","laboratory_tests":"MRI R\/L SHOULDER WITH CONTRAST"}
]

Ajax成功响应是:

<select class="form-control" id="yourSelect">

</select>

HTML

PropertyPlaceholderConfigurer

但我无法在选择标记中显示

4 个答案:

答案 0 :(得分:1)

尝试循环结果如下:

   success: function(data){
            // alert(data);
            console.log(data);
            var selOpts = "";

            $.each(data, function(k, v)
            {
                var id = data[k].laboratory_id;
                var val = data[k].laboratory_name;
                selOpts += "<option value='"+id+"'>"+val+"</option>";
            });
            $('#yourSelect').append(selOpts);
        }

答案 1 :(得分:1)

    $.ajax({
        url: config.routes.profitsReport,
        type: "POST",
        dataType: 'json',
        success: function (result) {
            $.each(result, function (i, value) {
                $('#category_profit').append('<option id=' + JSON.stringify(value.id) + '>' + JSON.stringify(value.name) + '</option>');
            });
        },
        error: function (request, status, error) {
            alert(request.statusText + "[" + request.status + "]");
            alert(request.responseText);
            $('button#form_salesReport_button').html(config.messages.searchReport);
        }
    });

答案 2 :(得分:1)

您可以循环播放

ar

通过

[
    {"laboratory_id":"19","laboratory_name":"ghc","laboratory_address":"cgc","laboratory_place":"jhggj","laboratory_tests":"MRI R\/L SHOULDER WITH CONTRAST"},
    {"laboratory_id":"20","laboratory_name":"BBNB","laboratory_address":"sdfds","laboratory_place":"sdfsd","laboratory_tests":"MRI R\/L SHOULDER WITH CONTRAST"},
    {"laboratory_id":"22","laboratory_name":"Anand","laboratory_address":"bsk","laboratory_place":"bengaluru","laboratory_tests":"MRI R\/L SHOULDER WITH CONTRAST"}
]

如果它是字符串,则可以通过var options = ""; for (let item in array) { options += `<option value=${item.id}>${item.laboratory_name}</option>`; } document.getElementById("yourSelect").innerHTML = options; 将其转换为数组。

答案 3 :(得分:0)

如果您希望来自ajax请求的json数据,那么您需要将dataType添加为json

$('[name=submits]').click(function(e)
{
    e.preventDefault();
    var array = [];
    $('select :selected').each(function(i,value)
    {
        array[i] = $(this).val();
    });
    var testdate = $("#appointmentdate10").val();
    //here make your ajax call to a php file
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "http://localhost/refer/index.php/details",
        data: { laboratory_tests: array, testdate: testdate },
        success: function(data){
            // alert(data);
            console.log(data);
            var selOpts = "";
            for (i=0;i<data.length;i++)
            {
                var id = data[i]['laboratory_id'];
                var val = data[i]['laboratory_name'];
                selOpts += "<option value='"+id+"'>"+val+"</option>";
            }
            $('#yourSelect').append(selOpts);
        }
    });
});