您的位置:首页 > 编程语言

AjAX基础入门级代码

2006-05-10 15:52 429 查看
<html>
<head>
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="0">
</head>
<body>
<form>
<p>City: <input type="text" name="city" id="city" size="25"
/></p>
<p>State: <input type="text" name="state" id="state" size="25"
/></p>
<p>Zip Code: <input type="text" name="zipCode" id="city" size="5" onclick="changenull()" /></p>
<input type="button" value="¸Ä±ä" onclick="changehave()" />
</form>
<script language="javascript" type="text/javascript">

/* 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();
}

if (!xmlHttp)
alert("Error initializing XMLHttpRequest!");

function getCustomerInfo() {
var phone = document.getElementById("phone").value;
var url = "/cgi-local/lookupCustomer.php?phone=" + escape(phone);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}

function updatePage() {
if (xmlHttp.readyState == 4)
if (xmlHttp.status == 200){
var response = xmlHttp.responseText.split("|");
document.getElementById("order").value = response[0];
document.getElementById("address").innerHTML =
response[1].replace(//n/g, "<br />");
}
else if (request.status == 404)
alert("Request URL does not exist");
else
alert("Error: status code is " + request.status);
}

var city = document.getElementById("city").value;
var state = document.getElementById("state").value;
// Only go on if there are values for both fields
if ((city == null) || (city == ""))
document.getElementById("city").value = "city";
if ((state == null) || (state == ""))
document.getElementById("state").value = "state";

function changenull(){
if (document.getElementById("city").value == "city")
document.getElementById("city").value = "";
}
function changehave(){
if (document.getElementById("city").value == "")
document.getElementById("city").value = "city";
}

</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: