2016-11-19 64 views
0

我使用笨使得自動完成搜索欄,我通過AJAX調用獲取數據,該數據被以陣列來作爲我使用命令的print_r檢查它()。數據進入陣列。追加陣列中的HTML(下降輸入場的向下)

現在阿賈克斯的數據也即將在執行console.log,這裏是我的Ajax代碼:

$(document).ready(function(){ 
    $('#country_id').keyup(function() { 
     var min_length = 0; 
    var keyword = $('#country_id').val(); 
    if (keyword.length >= min_length) { 
     $.ajax({ 
      url: 'http://localhost/new/index.php/travels/search_fields', 
      type: 'POST', 
      data: { term: $("#country_id").val()}, 
      success:function(data){ 
       console.log(data); 
      } 
     }); 
    } 
}); 
}); 

現在我要顯示在下拉下方的輸入字段數據。我現在應該怎麼做?請幫助我。

+0

的可能的複製[添加選項用JavaScript來選擇(http://stackoverflow.com/questions/8674618/adding-options-對選擇與 - 的JavaScript) –

回答

0

嘗試以下,

$(document).ready(function(){ 
$('#country_id').keyup(function() { 
    var min_length = 0; 
    var selectEl = $("<select id=\"selectId\" name=\"selectName\" />"); 
    var keyword = $('#country_id').val(); 
     if (keyword.length >= min_length) { 
      $.ajax({ 
       url: 'http://localhost/new/index.php/travels/search_fields', 
       type: 'POST', 
       data: { term: $("#country_id").val()}, 
       success:function(data){ 

        // Do this if returned data is not valid javascript array : var jArray = jQuery.makeArray(data); 
        var option = ''; 
        $.each(data, function (i, item) { 
       selectEl.appen($('<option value="'+ item + '">' + item + '</option>')); 
        }); 

       } 
      }); 
     } 
     //finally add select list below the input 
     $(this).after(selectEl); 
    }); 
}); 
+0

其追加空選擇標籤 –

+0

@HarrisKhan嘗試更新的答案,請 – ScanQR

0

試試這個

var select_data = ''; 
$.each(data, function (i, item) { 
    select_data += "<option value=''>'+item.some_value+'</option>";     
}); 
$(".your_append_identifier").append(select_data); 
+0

將在some_values什麼??? –