У меня есть текстовое поле. Когда я набираю что-то в текстовое поле, я хочу загрузить вещи в DIV ниже. Это мой эквивалент кода:
<input type="text" onkeyup="/* I am not sure how it's done, but I'll just write: */ document.getElementById('search_results').innerHTML = getContents('search.php?query='+this.value);" /><br/>
<div id="search_results"></div>
Надеюсь, вы можете помочь. Заранее спасибо!
РЕДАКТИРОВАТЬ: Я был бы признателен, если бы решение не включало использование jQuery - так долго, как это возможно.
ОТВЕТ: Как я это делаю:
<input type="text" onkeyup="loadSearchResults(this.value)" /><br/>
<div id="search_results"></div>
<script>
function loadSearchResults(query){
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
var xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("search_results").innerHTML=xmlhttp.responseText;
}else if(xmlhttp.readyState>=1 && xmlhttp.status==200){
// show the ajax loader or stuff like that...
}
}
xmlhttp.open("GET","search.php?search="+query,true);
xmlhttp.send();
}
</script>