<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>新用户注册</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script>
     var name = document.getElementById("regName").value;
    
     function onblurRegText() {
alert(name);
if(name == "haven") {
document.getElementById("divRegName").style.display = "inline";
document.getElementById("divRegName").innerText = "a";
}
     }
    
    </script> </head> <body>
<fieldset style="width: 500px">
<legend>
新用户注册
</legend>
<form name="registerForm">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" id="regName" style="width: 150px" value="a"
onblur="onblurRegText()">
<div id="divRegName" style="display: none"></div>
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="regPwd1" style="width: 150px">
</td>
</tr>
<tr>
<td>
确认密码:
</td>
<td>
<input type="password" name="regPwd2" style="width: 150px">
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
<input type="radio" name="regMale">

<input type="radio" name="regFemale">

</td>
</tr>
<tr>
<td>
出生日期:
</td>
<td>
<input type="text" name="regBirthDate" style="width: 150px">
</td>
</tr>
<tr>
<td>
E-Mail:
</td>
<td>
<input type="text" name="regMail" style="width: 150px">
</td>
</tr>
<tr>
<td>
所在地:
</td>
<td>
<select> </select>

<select> </select>

</td>
</tr>
<tr>
<td>
<input type="reset" name="regReset" value="重置">
<input type="submit" name="regSubmit" value="提交">
</td>
</tr>
</table>
</form>
</fieldset> </body>
</html>
alert(name);这个地方取不到值,把name放在函数里面就可以了,为什么

解决方案 »

  1.   

     <input type="text" id="regName" style="width: 150px" value="a" onblur="onblurRegText(this.value)">        <script>
            //这里当然取不到值,因为是再页面加载时运行的!
    //var name = document.getElementById("regName").value;
                
            function onblurRegText(name) {
    alert(name);
                if(name == "haven") {
                    document.getElementById("divRegName").style.display = "inline";
                    document.getElementById("divRegName").innerText = "a";
                }
            }
        
        </script>
      

  2.   

    赋值的时候,regName的input还没加载完毕。
    放到
    window.onload=function(){
      name = document.getElementById("regName").value;
    }
    中可以确保加载完毕。
      

  3.   

    javascript代码是从上往下执行的,当鼠标离开那个文本框时,他执行onblurRegText函数。他找不到name.