ユーザーが検索入力に何かを入力するたびに動作する、私のウェブサイトにはかなり基本的な自動提案機能があります。矢印キーを使用しない自動提案ドロップダウン
グーグルズ検索の自動提案のように機能すると思われます。しかし、いくつかの提案が返された場合、ユーザがキーボードの矢印の1つを押すと、それは何の提案も選択されません。ここで
は(提案の選択のための)私のJSです:ここでは
$(document).ready(function(){
var $result = $('li');
$('input').keydown(function(e){
var key = e.keyCode, $selected = $result.filter('.selected'), $current;
if(key != 40 && key != 38){
return;
}
$result.removeClass('selected');
if (key == 40){
if (!$selected.length || $selected.is(':last-child')){
$current = $result.eq(0);
} else {
$current = $selected.next();
}
} else if (key == 38){
if (!$selected.length || $selected.is(':first-child')){
$current = $result.last();
} else {
$current = $selected.prev();
}
}
$current.addClass('selected');
});
});
は私のAjaxです:
function findmatch(){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById('s-dif').innerHTML = xmlhttp.responseText;
}
}
var qVal = document.getElementById('query-value');
xmlhttp.open('GET', 'search/suggest.php?q=' + qVal.value, true);
xmlhttp.send();
}
非常に基本的なHTML:
<div class="form-container">
<form method="get" id="search" name="search">
<input type="text" name="q" id="query-value" onkeyup="findmatch();" placeholder="Search with Ajax...">
</form>
</div>
<div class="suggestions">
<ul id="s-dif">
</ul>
</div>
私は、問題は私の「選択のjQuery」内にあるかのように、しかし、私は
トリプルチェックすべてに確認しましたが、私は見つけることができないように感じる
if(isset($_GET['q'])){
$results = '';
$query = trim($_GET['q']);
if(!empty($query)){
$stmt = $conn->prepare("SELECT title, popularity FROM autosuggest WHERE keywords LIKE :query OR title LIKE :query ORDER BY popularity desc LIMIT 7");
$query = $query . '%';
$stmt->bindParam(':query', $query);
$stmt->execute();
$count = $stmt->rowCount();
$i = 0;
if($count > 0){
while($row = $stmt->fetch(PDO::FETCH_OBJ)){
$title = $row->title;
$popularity = $row->popularity;
$i++;
$results .= '<li id="select-suggest" value="' . $i . '" name="' . $title . '">' . $title . '</li>';
}
}
}
}
print("$results");
:
、そして最後に、私のPHP機能が動作しなくなるようなもの。
は、私はあなたが既にjQueryのを使用しているとき、あなたがあなた自身のAJAXを使用していることを笑うしなければなりません。 – PHPglue
@PHPglueこれは私の初めてのAjaxです。私はまだ私が見つけたウェブサイトから修正されたバージョンを使用しています... –
なぜ、オートコンプリートのtexboxを使用していませんか? –