Hola tengo un autocomplete. Los codigos no son mios, lo tome de internet.
Necesito que autoselecione el primero, para que al apretar enter seleccione la primera opcion y no envie el FORM.
Estos son los codigos:
Buscador:
Código HTML:
Ver original<script type="text/javascript" src="arabc/ajax_framework.js"></script> #search-wrap input{width:400px; font-size:16px; color:#999999; padding:6px; border:solid 1px #999999;}
#results{width:260px; border:solid 1px #DEDEDE; display:none;}
#results ul, #results li{padding:0; margin:0; border:0; list-style:none;}
#results li {border-top:solid 1px #DEDEDE;}
#results li a{display:block; padding:4px; text-decoration:none; color:#000000; font-weight:bold;}
#results li a small{display:block; text-decoration:none; color:#999999; font-weight:normal;}
#results li a:hover{background:#FFFFCC;}
#results ul {padding:6px;}
<input name="search-q" id="search-q" type="text" onKeyUp="javascript:autosuggest()" onKeyEnter=""/>
search.php
Código PHP:
Ver original<meta http-equiv="Content-type" content="text/html; charset=latin1" />
<?php
include('config.php');
$SQL_FROM = 'ar_abc';
$SQL_WHERE = 'AR_CODIGO';
$SQL_OR = 'AR_DESCRI';
?>
<?php
$getRecord_sql = 'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "%'.$searchq.'%" OR '.$SQL_OR.' LIKE "%'.$searchq.'%"';
echo '<ul>';
while ($row = mysql_fetch_array($getRecord)) {?><li><a href="#" onClick="fill('
<?php echo htmlentities($row['AR_CODIGO']);?>');return false;">
<?php echo htmlentities($row['AR_DESCRI']);?><small>CODIGO:
<?php echo htmlentities($row['AR_CODIGO']); ?></small></a></li>
<?php }
echo '</ul>';
?>
<?php } ?>
ajax_framework.js
Código Javascript
:
Ver originalfunction createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
function autosuggest() {
q = document.getElementById('search-q').value;
nocache = Math.random();
http.open('get', 'arabc/search.php?q='+q+'&nocache = '+nocache);
http.onreadystatechange = autosuggestReply;
http.send(null);
}
function autosuggestReply() {
if(http.readyState == 4){
var response = http.responseText;
e = document.getElementById('results');
if(response!=""){
e.innerHTML=response;
e.style.display="block";
} else {
e.style.display="none";
}
}
}
function clearsuggest() {
e = document.getElementById('results');
e.style.display="none";
}
function fill(i) {
e = document.getElementById('search-q');
e.value=i;
document.getElementById('results').style.display="none";
}
Gracias por leerlo