如何从jquery ajax成功回调填充下拉列表

时间:2014-07-07 19:15:12

标签: jquery ajax

我是jQuery和Javascript的新手。我有来自jquery ajax sucess回调的以下数据。

["Man","KFC","fsdfds","ETA","Star","SCity ","TESTGB"]  

现在,我想在成功回调中填充这些数据。

success: function(data) {
                // Call this function on success                
                alert(data);    
               var builder_arr=data.split(',');               
              for (var i=0;i<builder_arr.length;++i)  
              {
                 builder_arr[i]+':'+builder_arr[i];
              }     

        }

我不知道如何在选择内填充它 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

jsFiddle

<强> HTML

<select id="mydropdown">
</select>

<强> JS

function onSuccess(data) {
    $.each(data, function (index, value) {
        $('#mydropdown').append($('<option/>', { 
            value: value,
            text : value 
        }));
    }); 
}

var data = ["Man","KFC","fsdfds","ETA","Star","SCity ","TESTGB"];
onSuccess(data);

注意: 鉴于上述情况,这只是一个最好的猜测。