例子:一个注册页面用用户名和密码两个输入框。
需求:如果用户名为空,就调用focus()函数,将光标重新定位到用户名输入框;如果密码输入框为空,就将光标重新点位到密码输入框问题:如果当用户将光标切人用户名输入框,但是并没有填写就将光标切入密码输入框,这个时候光标就会上下来回的切换,知道卡死为止!请问如何做到这个效果。。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="txtUserName" onblur="OnCheckName()" /></td>
</tr>
<tr>
<td>Pwd:</td>
<td><input type="text" id="txtPwd" onblur="OnCheckPwd()" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="btnReg" onclick="OnSub()" value="注册" /></td>
</tr>
</table>
</body>
</html>
<script type="text/javascript">
var nameObj = document.getElementById("txtUserName");
var pwdObj = document.getElementById("txtPwd");

function OnCheckName(){
if(nameObj.value == ""){
nameObj.focus();
return false;
}
}

function OnCheckPwd(){
if(pwdObj.value == ""){
pwdObj.focus();
return false;
}
}

function OnSub(){
if(OnCheckName && OnCheckPwd){
return true;
}else{
return false;
}
}
</script>

解决方案 »

  1.   


        function OnSub(){
            if(OnCheckName && OnCheckPwd){
                return true;
            }else{
               while(true){
                  nameObj.focus();
                  pwdObj.focus();           }
            }    
        }
    要卡死?
      

  2.   


    对啊,你这样是在提交的时候验证,这样写估计不会吧!!!但是每个输入框的onblur()时间里面也需要判断啊。。
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>无标题文档</title></head><body>
    <table>
        <tr>
            <td>Name:</td>   //最好加上name吧
            <td><input type="text" id="txtUserName" name="txtUserName" onblur="OnCheckName()" /></td>
        </tr>
        <tr>
            <td>Pwd:</td>
            <td><input type="text" id="txtPwd" name="txtPwd" onblur="OnCheckPwd()" /></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" id="btnReg" onclick="OnSub()" value="注册" /></td>
        </tr>
    </table>
    <script type="text/javascript">
        var nameObj = document.getElementById("txtUserName");
        var pwdObj = document.getElementById("txtPwd");
        
        function OnCheckName(){      
            if(nameObj.value == ""){
                nameObj.focus();
                return false;
            }       
    //只有返回false;没有true,提交时不继续往下执行
            return true;
        }
        
        function OnCheckPwd(){
         if(pwdObj.value == ""){
                pwdObj.focus();
                return false;
            }        
    //只有返回false;没有true,提交时不继续往下执行
            return true;
        }
        
        function OnSub(){
    // if(OnCheckName() && OnCheckPwd())这里要加括号
            if(OnCheckName() && OnCheckPwd()){
                return true;
            }else{
                return false;
            }    
        }
    </script>
    </body>
    </html>
      

  4.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>无标题文档</title></head><body>
    <table>
        <tr>
            <td>Name:</td>   //最好加上name吧
            <td><input type="text" id="txtUserName" name="txtUserName" onblur="OnCheckName()" /></td>
        </tr>
        <tr>
            <td>Pwd:</td>
            <td><input type="text" id="txtPwd" name="txtPwd" onblur="OnCheckPwd()" /></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" id="btnReg" onclick="OnSub()" value="注册" /></td>
        </tr>
    </table>
    <script type="text/javascript">
        var nameObj = document.getElementById("txtUserName");
        var pwdObj = document.getElementById("txtPwd");
        
        function OnCheckName(){      
            if(nameObj.value == ""){
                nameObj.focus();
                return false;
            }       
    //只有返回false;没有true,提交时不继续往下执行
            return true;
        }
        
        function OnCheckPwd(){
         if(pwdObj.value == ""){
                pwdObj.focus();
                return false;
            }        
    //只有返回false;没有true,提交时不继续往下执行
            return true;
        }
        
        function OnSub(){
    // if(OnCheckName() && OnCheckPwd())这里要加括号
            if(OnCheckName() && OnCheckPwd()){
                return true;
            }else{
                return false;
            }    
        }
    </script>
    </body>
    </html>
      

  5.   


    function Check(){          
            if(nameObj.value == ""){
                nameObj.focus();
                return false;
            } 
            else  if(pwdObj.value == ""){
                pwdObj.focus();
                return false;
            } 
             return true;
        }
        
           
      
      

  6.   

    FF,IE里刚刚好像是卡住了,
    我暂时没考虑你的那个光标,
    再试试
      

  7.   

    最简单的,在onblur=""后面加 return false; 就可以了如:onblur="OnCheckName(); return false;"
      

  8.   

    最好的也是不要用submit,改用<input type="button">
    在check方法里面加document.form[0].submit();这样提交会好点,
    避免出现那样子的问题、
      

  9.   


    我不是要用submit。我是需要在输入框的onblur事件后面加那个方法。。为什么在被调用的方法没用用呢??
      

  10.   

    嗯,那就用onblur="OnCheckName(); return false;"这个吧,
    如果考虑其它的话,
    在<input type="submit" onclick="***; return false">也加上这个吧,
      

  11.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>无标题文档</title>
    </head><body>
    <table>
        <tr>
            <td>Name:</td>   //最好加上name吧
            <td><input type="text" id="txtUserName" name="txtUserName" onblur="return OnCheck();" /></td>
        </tr>
        <tr>
            <td>Pwd:</td>
            <td><input type="text" id="txtPwd" name="txtPwd" onblur="return OnCheck();" /></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" id="btnReg" onclick="return OnCheck();" value="注册" /></td>
        </tr>
    </table>
    <script type="text/javascript">
        var nameObj = document.getElementById("txtUserName");
        var pwdObj = document.getElementById("txtPwd");
        function OnCheck() {
            if (nameObj.value == "") {
                nameObj.focus();
                return false;
            }
            else if (pwdObj.value == "") {
                pwdObj.focus();
                return false;
            }
            else {
                alert(nameObj.value + pwdObj.value);
            }
        }
    </script>
    </body>
    </html>我试过了,完全可以!!
      

  12.   

    没看懂要做什么,不过表单提交验证前的检查可以这样:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>无标题文档</title>
    </head><body>
    <form id="form1" name="form1" action="reg.asp" method="post" onsubmit="javascript:return checkSubmit();">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" id="txtUserName" name="txtUserName" onblur="return OnCheck();" /></td>
        </tr>
        <tr>
            <td>Pwd:</td>
            <td><input type="text" id="txtPwd" name="txtPwd" onblur="return OnCheck();" /></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" id="btnReg" value="注册" /></td>
        </tr>
    </table>
    </form><script type="text/javascript">
        var nameObj = document.getElementById("txtUserName");
        var pwdObj = document.getElementById("txtPwd");
        function checkSubmit() {
            if(nameObj.value.replace(/ /g,"") == "") {
                alert("姓名不能留空!");
                nameObj.focus();
                return false;
            }
            if(pwdObj.value.replace(/ /g,"") == "") {
                alert("密码不能留空!");
                pwdObj.focus();
                return false;
            }
            alert(nameObj.value + pwdObj.value);
            return true;
        }
    </script>
    </body>
    </html>表单要用form标签括起来,最好是用form来提交。如果要用ajax提交,checkSubmit()里总是返回false,就可以防止表单提交导致页面刷新了。
      

  13.   

    很简单的一个竞争策略你会死循环是因为 2个框的权重一样 也就是没办法决策出最终的获取者所以 名字框 和 密码框 的 blur 事件 要调用同一个方法在该方法中 决定到底哪个框 focus();业务流程就是这样 至于 决定哪个框 focus()就是你的业务逻辑了
      

  14.   

    试试在判断密码是否为空的函数中先判断下用户名是否为空,为空则返回false并获取焦点,然后再判断密码fn(){
    if(用户名==null)用户名.focus;
    else if(密码==null)密码.focus;
    }
    试试这样写,肯定没问题了。