2011-08-02 31 views
0

我在這個AJAX的東西總小白麻煩,但我不知道怎麼一會去這樣做這樣的事情: http://www.w3schools.com/php/php_ajax_php.asp使用Ajax調用數據庫 - 具有使輸入上點擊

但與事件發生在點擊一個按鈕上。這裏是代碼的副本:

<html> 
<head> 
<script type="text/javascript"> 
function showHint(str) 
{ 
if (str.length==0) 
{ 
document.getElementById("txtHint").innerHTML=""; 
return; 
} 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
    } 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
    xmlhttp.open("GET","gethint.php?q="+str,true); 
    xmlhttp.send(); 
    } 
</script> 
</head> 
<body> 

<p><b>Start typing a name in the input field below:</b></p> 
<form> 
First name: <input type="text" onkeyup="showHint(this.value)" size="20" /> 
</form> 
<p>Suggestions: <span id="txtHint"></span></p> 

</body> 
</html> 

我試圖用一個onclick添加輸入按鈕,但我不能讓它通過輸入文本框的值。

回答

0

添加ID輸入字段,並通過document.getElementById(「TEXTBOXID」)。值訪問該值

+0

太感謝你了! – ToryT