0
对,这很奇怪,我不知道为什么会发生这种情况,但我正在做的是我有一个搜索字段,用户输入一个名称,我希望它以显示建议,现在这是可行的,但是当用户使用箭头键(向上或向下)时,对象应该是高亮的,基本上就像标准自动完成,只是具有不同的设计。当按下箭头键时,JS Div没有改变颜色
问题是高光位,当我按下向下箭头时,只要按住箭头键,对象就会亮起来,但它不会停留在高光。我想不通为什么,我下面贴相关的代码,我希望有人能够帮助:
主文件(文本框):
<div style="position: relative; top: 0px; left: -51px; width: 200px;">
<form method="GET" action="" style="padding: 0px; padding-right: 10px; margin: 0px;">
<input type="text" id="search_field" onkeyup="javascript: suggestions(this.value);" autocomplete="off" style="border: 0px solid #ffffff; width: 250px; padding-left: 20px; color: #ffffff; height: 30px; border-bottom: 1px solid #333333; background: #222222;" name="q" placeholder="Search MVDb">
</form>
<div style="position: absolute; left: 2px; top: 8px;">
<img height="15" src="img/search.png">
</div>
</div>
<div id="result" style="z-index: 100; position: absolute; cursor: pointer; top: 86px; right: 5px; width: 251px; background: #eeeeee;">
</div>
与ID结果DIV就是这些建议会被加载进去。
主文件(jQuery的/ JS):
function suggestions(value){
$.ajax({
url: 'get_suggestion.php?q='+value,
success: function(data) {
$('#result').html(data);
}
});
}
$(document).ready(function() {
$("#search_field").keydown(function(e) {
if (e.keyCode == 40) {
//DOWN
document.getElementById('search_result_0').style.background = "#333333";
e.preventDefault();
return false;
}
else if (e.keyCode == 38) {
//UP
e.preventDefault();
return false;
}
});
});
get_suggestion.php:
<?php
include 'js/db/db.php';
$value = $_GET["q"];
if($value==""){
}
else{
$value = trim($value);
$query="select * from movies where title like \"%$value%\" LIMIT 6";
$rt=mysql_query($query);
echo mysql_error();
$counter = -1;
while($nt=mysql_fetch_array($rt)){
$counter = $counter+1;
?>
<div id="search_result_<?php echo $counter ?>" cellspacing="0" cellpadding="0" onClick="window.location = 'movie.php?movie=<?php echo $nt['title']?>'" width="100%" style="color: #333333; cursor: pointer;">
<table cellspacing="0" cellpadding="0">
<tr valign="top">
<td style="padding: 5px; width: 30px;">
<img height="60" src="http://210.177.0.75/mvdb/img/covers/<?php echo $nt['title'] ?>.jpg">
</td>
<td align="left" style="padding: 5px;">
<?php
$title = substr($nt[title], 0, 17);
if($title == $nt[title]){
}
else{
$title = $title."...";
}
?>
<font style="font-family: Arial; font-size: 15px; font-weight: bold;"><?php echo $title ?></font> <img src="img/age/BBFC <?php echo $nt['bbfc_age'] ?>_small.png"><br>
<font style="font-family: Arial; font-size: 12px; color: #aaaaaa;">Tom Cruise, Michelle Monaga</font>
<font style="font-family: Arial; font-size: 12px; color: #aaaaaa;">Released: 4 May 2006</font>
</td>
</tr>
</table>
</div>
<?php
}
}
?>
我试图格式化代码,以及可能的。我认为这可能是当用户按下箭头键时,ajax请求被触发,但这不会有任何意义,因为我正在使用e.preventDefault()来阻止这一点,有人知道为什么会发生这种情况吗?
好的:)在你发布你的答案之前,我设法解决了这个问题,但是我刚刚意识到这一点以及......愚蠢的我。感谢您清理它,花时间阅读相当长的问题,虽然:) –