我观看了一个视频,该视频描述了如何在输入中实现自动完成。最后,我在 Firefox 和 chrome 中进行了测试,它运行良好,除了在 Internet Explorer 中:这是下面的代码:
jQuery:
function getSuggestions(value){
if(value!=''){
$.get('suggest.php', {requestKword:value}, function (data){
$('#suggestions').show();
$('#suggestions').html(data);
doCSS();
});
}
else{
removeSuggestions();
}
}
function removeSuggestions(){
$("#suggestions").html("");
undoCSS();
}
function fill(thisValue) {
$('#ii').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
function doCSS(){
$("#suggestions").css({
'border' : 'solid',
'border-width':'1px',
'position': 'relative'
});
}
function undoCSS(){
$("#suggestions").css({
'border' : '',
'border-width':''
});
}
function addText(value){
$("#ii").val(value);
}
CSS:
#suggestions{
text-align:left;
padding-left:5px;
font-size:14px;
}
#link:hover{ background-color:#f0f0f0; cursor:default;}
html:
<form action="/" method="get" id="set" name="set">
<input id="ii" type="text" name="q" size="96px" value="" onblur="fill();" onkeyup="getSuggestions(this.value)" /><br />
<div id="suggestions"></div>
</form>
mysql:建议.php
mysql_connect('localhost', 'root', '');
mysql_select_db('keywords') or die (mysql_error());
$keyword=mysql_real_escape_string($_GET['requestKword']);
$result = mysql_query("SELECT * FROM `table`.`keys` where `column` like '$keyword%' LIMIT 0 , 10") or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
echo '<div id="link" onclick="addText(\''.$row['keyword'].'\')" ">'.$row['keyword'].'</div>';
}