用 JavaScript 代碼捕獲和設置字段值 //獲取 id 值為 first_name 的表單域
var name = document.getElementById('first_name').value;//修改 id 值為 test 的表單域的值
document.getElementById('test').value = response[0];
在 Microsoft 瀏覽器上創建 XMLHttpRequest 對象 var xmlHttp = false;
try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (e2) {xmlHttp = false;}
}
以支持多種瀏覽器的方式創建 XMLHttpRequest 對象 /* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (e2) {xmlHttp = false;}
}
@end @*/if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {xmlHttp = new XMLHttpRequest();
}
開始工作:發送Ajax請求 function callServer() {// Get the city and state from the web formvar city = document.getElementById("city").value;var state = document.getElementById("state").value;// Build the URL to connect tovar url = "/scripts/getZipCode.php?city=" + escape(city) + "&state=" + escape(state);// Open a connection to the serverxmlHttp.open("GET", url, true);// Setup a function for the server to run when it's donexmlHttp.onreadystatechange = updatePage;// Send the requestxmlHttp.send(null);
}