[英]Append array in html (drop down of input field )
我正在使用codeigniter制作自動完成搜索欄,我通過ajax調用獲取數據,當我使用命令print_r()對其進行檢查時,數據正以數組形式進入。 數據進入數組。
現在在ajax中,數據也出現在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);
}
});
}
});
});
現在,我想在輸入字段下方的下拉列表中顯示該數據。 我現在應該怎么辦? 請幫幫我。
嘗試跟隨,
$(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);
});
});
嘗試這個
var select_data = '';
$.each(data, function (i, item) {
select_data += "<option value=''>'+item.some_value+'</option>";
});
$(".your_append_identifier").append(select_data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.