-
问题内容:问题出在这里,如果去掉此语句(obj.value==response)的值总是false!想不明白
- 原讨论链接:http://community.csdn.net/expert/topicview1.asp?id=5394829
- 所属论坛:Ajax
审核组:WEB开发
- 提问者:sangok
解决者:net_lover
- 感谢:net_lover zhulei2008
- 关键字:Web 开发 Ajax
- 答案:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<style type="text/css">
<!--
#needpass {
float: none;
height: 200px;
width: 400px;
background-color: #CCFFCC;
border: 1px dashed #99CC00;
}
#text {
font-family: Arial, Helvetica, sans-serif;
font-size: 50px;
font-style: italic;
font-weight: bolder;
color: #FF6600;
text-decoration: underline;
}
#sh {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bolder;
color: #FF0000;
position: absolute;
top: 188px;
left: -100px;
width: 515px;
font-style: normal;
}
input {
border: 1px double #000000;
color: #000000;
background-color: #66CCFF;
}
-->
</style>
<script language="javascript" type="text/javascript">
function check()
{
var response;
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
var url = "password.inc";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = upPage;
xmlHttp.send(null);
function upPage(){
if (xmlHttp.readyState == 4) {
response = xmlHttp.responseText;
}
}
var obj=document.form1.password;
alert("Login in checking") //=------------问题出在这里,如果去掉此语句(obj.value==response)的值总是false
if (obj.value==response){
alert("succeed")
window.close(this)
window.opener.use.value="通过验证"
window.opener.tu.style.display="block";
//window.opener.location="f-map.htm"
//window.close(this)
}
else{
alert("Wrong Password or not input");
}
//}
}
</script>
</head>
<body>
<div align="center">
<div id="needpass" align="center">
<br>
<div align="center" id="text">Need Password</div>
<div align="center">
<form name="form1" id="form1" method="post" action="map.html">
<input type="password" name="password" />
<input type="button" name="Submit" value="查看" onClick="check();" />
</form>
</div>
</div>
</body>
</html>
---------------------------------------------------------------
function upPage(){
if (xmlHttp.readyState == 4) {
response = xmlHttp.responseText;
var obj=document.form1.password;
obj.value==response
}
}
你的操作是异步的,没有等到成功就赋值了。
---------------------------------------------------------------
正如楼上说的,你的那个根本是取不到值的,状态值还没返回成功,你就赋值了,肯定是==false的,
只有当异步完成的时候,也就是xmlHttp.readyState == 4的时候,你才可以对其服务器端返回的值进行赋值操作
- 评价:
给朵鲜花(7)
扔个鸡蛋(2)