當用戶在上面的下拉列表中選擇某個客戶時,會執行名為 "showCustomer()" 的函數。該函數由 "onchange" 事件觸發:
function showCustomer(str)
{
var xmlhttp;
if (str=="")
{
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","getcustomer.asp?q="+str,true);
xmlhttp.send();
}
showCustomer() 函數執行以下任務:
- 檢查是否已選擇某個客戶
- 創建 XMLHttpRequest 對象
- 當服務器響應就緒時執行所創建的函數
- 把請求發送到服務器上的文件
- 請注意我們向 URL 添加了一個參數 q (帶有輸入域中的內容)