Chrome,Opera,Safari上“回车”->“Ajax提交”失败的案例、原因及解决方案

<html>
 <head>
 function resetIdAndPwd(){
document.getElementById("num").value="";
document.getElementById("pwd").value="";
}
function keypressEvent(){
var keyCodeT=0;
if(navigator.appName.toLowerCase().indexOf("netscape")>=0){//是Chrome或FireFox
keyCodeT=keypressEvent.caller.arguments[0].keyCode;
}
else{//IE或Opera
keyCodeT=window.event.keyCode;
}
if(keyCodeT==13)
doLogin();
}
function doLogin(){
var xmlHttpT=getAjaxObject();
if(xmlHttpT==null){
alert("登陆失败!");
return;
}
var idT=document.getElementsByTagName("meta")[0].content;
var numT=document.getElementsByName("num")[0].value;
var pwdT=document.getElementsByName("pwd")[0].value;
var getdata="?num="+numT+"&pwd="+pwdT+"&id="+idT;
xmlHttpT.open("GET","CheckLogin.do"+getdata+"&_1="+Math.random(),true);
xmlHttpT.onreadystatechange=function(){
if(xmlHttpT.readyState==4&&xmlHttpT.responseXML!=null){
if(xmlHttpT.responseXML.documentElement.getElementsByTagName("check-code")[0].childNodes[0].nodeValue==1){
alert("登录失败!");
document.getElementById("num").value="";
document.getElementById("pwd").value="";
}
else{
var newUrl=xmlHttpT.responseXML.documentElement.getElementsByTagName("new-url")[0].childNodes[0].nodeValue;
location.replace(newUrl);
}
}
}
xmlHttpT.send(null);
}
</head>
<body>
<form>
<span>请输入登陆信息:</span>
<span>学号:</span><input type="text" name="num" id="num" align="left" onkeypress="keypressEvent();" />
<span>密码:</span><input type="password" name="pwd" id="pwd" size="20" onkeypress="keypressEvent();"/>
<input name="login" type="button" id="login" value="登陆" onClick="doLogin();" />
<input name="reset" type="button" id="reset" value="重填" onclick="resetIdAndPwd();"/>
</form>
</body>
</html>
在chrome、opera、safari(这个浏览器我没有测试过)上将失败。解决方法就是把form去掉
(我说的“失败”就是“if(xmlHttpT.readyState==4&&xmlHttpT.responseXML!=null)”在绝大多数情况下是false )
参考文献:
http://tieba.baidu.com/f?kz=669494938 
http://www.neatstudio.com/show-1110-1.shtml 
posted @ 2010-04-18 16:57  广岛之恋  阅读(747)  评论(0)    收藏  举报