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

那位大神能帮我看看为什么getElementById(id) 取不到值呢
<script language="javascript" type="text/javascript" src="login.js"></script></head><body>
<div width=1000px;>
<table border=0>
<tr><td>注册名称</td><td><input type="text" id="lgname" name="lgname"></input></td></tr>
<tr><td>注册密码</td><td><input type="password" id="login_pwd2" name="loginpwd2"></input></td></tr><tr><td>验&nbsp;证&nbsp;码</td><td><input type="text" id="lgchk" name='lgchk'></input></td><td><img id="chkid" src="" width="55" height="18"></img></td><td><a id="changea" href="#">看不清</a></td></tr>
<tr><td><input type="hidden" name="chknm" id="chknm" value=""></input></td></tr>
<tr><td><input type="button" id="lgbtn" value="登录"></input></td><td><input type="button" id="reg" value="用户注册"></input></td><td><input type="button" id="found" value="找回密码"></input></td></tr>
</table>


点击登录调用login.js文件
下面是login.js文件function $(id){
return document.getElementById(id);
}
$('lgbtn').onclick=chklg;

function chklg(){
if($('lgname').value.match(/^[a-zA-Z_]\w*$/)==null){
alert('请输入合法名称');
$('lgname').focus();
return false;
}

if($('lgname').value==''){
alert('请输入用户名!');
$('lgname').focus();
return false;
}

if($('login_pwd2').value=''){
alert('请输入密码!');
$('login_pwd2').focus();
return false;
}

if($('lgchk').value==''){
alert('请输入验证码');
$('lgchk').focus();
return false;
}

if($('lgchk').value!=$('chknm').value){
alert('验证码输入错误');
$('lgchk').focus();
return false;
}

count=document.cookie.split(';')[0];
if(count.split('=')[1]>=3){
alert('因为您的非法操作,您将无法再执行登陆操作');
return false;
}
alert(document.getElementById('login_pwd2').value);//alert(document.getElementById('loginpwd2'));url='login_chk.php?act='+(Math.random())+'&name='+$('lgname').value+'&pwd'+$('login_pwd2').value;
xmlhttp.open('get',url,true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readystate==4){
  if(xmlhttp.status==200){
  msg=xmlhttp.responseText;
  if(msg=='0'){
  alert('您还没有激活,请先登录邮箱进行激活操作。');
  }else if(msg=='1'){
  alert('用户名或密码输入错误,您还有2次机会');
  $('lgpwd').select();
  }else if(msg=='2'){
  alert('用户名或密码输入错误,你还有1次机会');
  $('lgpwd').select();
  }else if(msg=='4'){
  alert('用户名输入错误');
  $('lgname').select();
  }else if(msg=='-1'){
  alert('登录成功');
  location='main.php';
  }else{
  alert(msg);
  }  
  }
  }
}
xmlhttp.send(null);
}

能取到对象,但取不到值

------解决方案--------------------
你<script language="javascript" type="text/javascript" src="login.js"></script>
放在HTML的前面了。所以,当执行:
$('lgbtn').onclick = chklg;
的时候,根本还没有lgbtn这个对象。所以自然无效。
解决方法有两种:
1.把这条语句放在HTML的最后面。
2.修改login.js,把代码放在window.onload事件中.

对楼主来说,用第一种方法就行了。
------解决方案--------------------
探讨
你<script la