php - 在选择选项标签中循环 Ajax 响应

标签 php jquery json ajax codeigniter

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

Ajax 脚本

$('[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",
        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);
        }
    });
});

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"}
]

html

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

</select>

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

最佳答案

    $.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);
        }
    });

关于php - 在选择选项标签中循环 Ajax 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43868298/

相关文章:

jquery 中的 javascript:void(0)

javascript 日期时间比较

php - 如何使用 PHP 获取列中的所有值?

php - 带有隐藏字段的输入表单如何保护它

jquery - tinyMCE 文本区域自动高度

json - 创建 JSON 数组和 JSON 对象

android - 如何使座位布局像redbus?

php - 在数据库中有唯一的文本

javascript - 使用ajax jquery将变量传递给php页面?

c# - 有没有办法登记 Azure 服务总线中所有队列的所有消息并将它们存储在 json 中?