duanpao9781 2014-04-08 15:37
浏览 53
已采纳

通过AJAX拉入JSON以填充下拉列表

I am pulling in some JSON data that will vary... for instance:

Data returned could be:

[{"userID":"2779","UserFullName":" Absolute Pro-Formance"},{"userID":"2780","UserFullName":" AR Fabrication"},{"userID":"2781","UserFullName":" Banda Lucas Design Group"}]

or:

[{"orderID":"112958","OrderName":"Order ID: 112958"},{"orderID":"112957","OrderName":"Order ID: 112957"},{"orderID":"112956","OrderName":"Order ID: 112956"}]

What I am attempting to do is process this JSON to build a <select> list.

// Load in a drop-down as JSON
function LoadDropDown($url, $where, $id, $selectName){
    var $loading = '<div class="pageLoader" style="margin:0 auto !important;padding:0 !important;"><img src="/assets/images/ajax-loader.gif" alt="loading..." height="11" width="16" /></div>';
    var $t = Math.round(new Date().getTime() / 1000);
    var $container = jQuery($where);
    var options = {
            url: $url + '?_=' + $t,
            cache: false,
            type: 'POST',
            beforeSend: function(){
                    $container.html($loading);  
                },
            success: function(data, status, jqXhr){
                $html = '<select class="form-control" id="'+$selectName+'" name="'+$selectName+'">';
                $html += '<option value="0">- Select an Option -</option>';
                for(var i = 0; i < data.length-1; ++i) {
                    var item = data[i];
                    console.log(item.userID);
                }
                $html += '</select>';
                $container.html('<pre>' + data + '</pre>');
            },
            complete: function(jqXhr, status){},
            error: function(jqXhr, status, error){
                $container.slideDown('fast').html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><i class="fa fa-exclamation-triangle fa-4x pull-left"></i><p><strong>Danger Will Robinson!</strong><br />There was an issue pulling in this page. Our support team has been notified, please check back later.</p></div>');    
            }
    };
    jQuery.ajax(options);
}

The issue I am having is... #1 console.log(item.userID); always shows undefined, and #2 how can I effecitvely dynamically build the options? The returned JSON will ALWAYS contain 2 items per row and id, and a name

UPDATE

for(var $key in data){
    var $val = data[$key];
    for($j in $val){
        console.log('name:' + $j + ' = ' + $val[$j]);
    }
}

Is showing me what I need in Firefox Console... But 1 item per line, for each (for example the 1st JSON) name:userID = 1234 next line name:UserFullName = TheName

How can I get them so I can build my <options>?

With:

for(var k in data) {
    console.log(k, data[k]);
}                   

I am returned:

2955 Object { orderID="8508", OrderName="Order ID: 8508"}

and

2955 Object { userID="1355", UserFulleName="Me Myself And I"}
  • 写回答

3条回答 默认 最新

  • drlq92444 2014-04-08 17:32
    关注

    Cured: Changed my loop to cycle through each item in the returned JSON, got their keys, etc...

    var $dl = data.length;
    for(var $i = 0; $i < $dl - 1; ++$i) {
        var $keys = Object.keys(data[$i]);
        $html += '<option value="' + data[$i][$keys[0]] + '">' + data[$i][$keys[1]] + '</option>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大