1
我想在我的表單中使用jQuery,PHP和MySQL使用multiple autocomplete。但它不適合我。我有什麼錯誤嗎?爲什麼選擇使用jQuery和PHP自動填充不起作用?
HTML代碼:
<input type="text" id="country" name="country" />
jQuery代碼:
$("#countries")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("autocomplete").menu.active) {
event.preventDefault();
}
})
.autocomplete({
source: function(request, response) {
$.getJSON("process/find_countries.php", {
term: extractLast(request.term)
}, response);
},
search: function() {
// custom minLength
var term = extractLast(this.value);
if (term.length < 2) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
PHP代碼
$result = array();
$term = strtolower($_GET["term"]);
$sql = "SELECT title FROM tbl_countries WHERE title LIKE ?";
$q = $db->prepare($sql);
$q->execute(array('%'.$term.'%'));
$rows = $q->rowCount();
echo($rows);
while ($r = $q->fetch())
{
array_push($result,array('label'=>$r['title'], 'value'=>$r['title']));
}
echo json_encode($result);
控制檯中顯示的任何錯誤? – 2012-07-27 13:34:10
@wirey:Nothing ... – 2012-07-27 13:37:30
in the url ..刪除最後一部分並添加'process/find_countries.php'來查看顯示內容 – 2012-07-27 13:39:30