0
我想使用jQuery autocomplete plugin with remote data source,我想我得到了我想要的結果,但是我無法在JavaScript代碼中訪問它們。如何從jQuery自動完成獲取所有結果?
<div class="ui-widget">
<label for="search_courses">Search:</label>
<input id="search_courses" />
</div>
<div class="ui-widget" style="margin-top:2em; font-family:Arial">Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>
<script type="text/javascript" id="autoCompleteScript">
$(function() {
function log(message) {
$("<div>").text(message).prependTo("#log");
$("#log").scrollTop(0);
}
$("#search_courses").autocomplete({
source: "search_datasource",
minLength: 2, //search after two characters
select: function(event, ui) {
// This is never called, because there is nothing to select.
alert("test. code does not come here.");
log(ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value);
}
});
$("#search_courses").on("autocompletesearch", function(event, ui) {
console.log(ui); // see the picture for this output
});
});
</script>
出於測試目的,我的search.php(笨)是這樣的:
$this->load->database();
$this->load->database();
$result = $this->db->query("SELECT id as label, title as value FROM mytable")->result_array();
$response = "";
$response_array=array();
foreach($result as $row)
{
$response_array[] = json_encode($row);
}
$response = implode("," , $response_array);
$response = "[". $response ."]";
echo $response; // it echoes something in this form :
//[{"id":"Phoenicopterus roseus","label":"Greater Flamingo","value":"Greater Flamingo"},{"id":"Tarsiger cyanurus","label":"Red-flanked Bluetail","value":"Red-flanked Bluetail"}]
// but it contains 60 mysql rows.
現在,請參閱當我做搜索會發生什麼:
那麼我怎樣才能得到所有j的數組由PHP打印的兒子對象?
我的意思是,我如何訪問JS中的數組?
感謝您的幫助!
謝謝,但它給人的錯誤:'語法錯誤:JSON.parse:意外character'我覺得UI已經是一個對象。這不是search.php的迴應。 – jeff