日期:2014-05-16  浏览次数:20979 次

onreadystatechange没有执行
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<script type="text/javascript">
function up()
{
var xmlHttp;
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
} else 
{
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
xmlHttp.open("post","ajax01.php",true);
xmlHttp.onreadystatechange=changed;
alert(xmlHttp.readyState);
xmlhttp.send();
alert(xmlHttp.readyState);
}
function changed()
{
if(xmlHttp.readyState=4 || xmlHttp.status=200)
{
alert(5);
var value = document.getElementById("input1").value;
}
}
</script>
</head>
<body>
<form>
username:<input id="input1" name="username" /><br>
<input type="submit" value="OK" onclick="up()" />
</form>
</body>
</html>

ajax01.php代码:
<?php
echo("hello world!");
?>
[code=HTML][/code]

------解决方案--------------------
HTML code

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<script type="text/javascript">
var xmlHttp;
function up()
{
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
} else  
{
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}

xmlHttp.onreadystatechange=changed;
xmlHttp.open("post","ajax01.php",true);
xmlHttp.send();
}
function changed()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
alert(5);
var value = document.getElementById("input1").value;
}
}
</script>
</head>
<body>
<form>
username:<input id="input1" name="username" /><br>
<input type="button" value="OK" onclick="up()" />
</form>
</body>
</html>