2017-10-16 5 views
0

的jQuery:的jQuery UI的自動完成數據未顯示

function search($id, $column) { 
    $("#" + $id).autocomplete({ 
    source: function(request, response) { 
     jQuery.post("<?php echo base_url().'index.php/Auto_complete/item_search'?>", { 
     Query: request.term, 
     column: $column 
     }, function(data) { 
     response(data); 
     }); 
    } 
    }); 
} 

HTML:

<input type="text" class="form-control English_name" id="English_name" name="English_name" placeholder="Enter Item Name (English)" onchange="product_name()" onkeyup="search('English_name', 'English_name')" autocomplete="off"> 

AJAX輸出:

["Styler King 6 Card Holder (Set of 1, Khaki)","ds 18 sticker","famous sticker","wx sticker","lk small sticker","LX Sticker"] 

任何人都可以請幫我爲什麼數據沒有顯示和如何我可以在jQuery UI自動完成中顯示數據嗎?

回答

0

目前還不清楚你試圖用你的代碼來完成什麼。

這裏是你可能會發現有用的例子:

https://jsfiddle.net/Twisty/t1ruyg11/

的JavaScript現在

var myData = [ 
    "Styler King 6 Card Holder (Set of 1, Khaki)", 
    "ds 18 sticker", 
    "famous sticker", 
    "wx sticker", 
    "lk small sticker", 
    "LX Sticker" 
]; 

$(function() { 
    $("#English_name").autocomplete({ 
    source: function(req, resp) { 
     $.post("/echo/json/", { 
     json: JSON.stringify(myData) 
     }, function(data) { 
     resp($.ui.autocomplete.filter(data, req.term)); 
     }); 
    } 
    }); 
}); 

,考慮你的代碼,你可能會想嘗試像下面這樣的東西:

function search(t, c){ 
    var url = "<?php echo base_url().'index.php/Auto_complete/item_search'; ?>"; 
    var query = { 
    Query: t.trim(), 
    column: c 
    }; 
    $.post(url, query, function(results){ 
    if(results.length){ 
     return results; 
    } else { 
     return []; 
    } 
    }); 
} 

$(function(){ 
    $("#English_name").autocomplete({ 
    source: function(request, response){ 
     response(search(request.term), $column); 
    } 
    }); 
});