如何获得表单焦点

a4321262 2013-02-21 11:57:53
例如:当邮箱错误的提示框出来后,点玩确定又重新跑到此文本框来重新获得焦点
我用的是onblur事件注册的函数,当失去焦点的时候触发checkemail函数,尽管写了document.getElementById("eml").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>
<link href="style/style2.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
//检查邮箱
function checkemail(){
var myemail=document.getElementById("eml").value;
var mesg_error= document.getElementById("emai_error");
if(myemail!="")
{//查找@,如果没有就返回-1
if(myemail.indexOf("@")==-1||myemail.indexOf(".")==-1||(myemail.indexOf("@")>myemail.indexOf(".")))
{
//mesg_error.innerHTML="<font color='red'></font>";
alert("email格式不正确!例:xiaoniu@qq.com");
document.getElementById("eml").focus();
return false;
}
else
{
mesg_error.innerHTML="<font color='red'>√</font>";
return true;
}
}
else
{
alert("email不能为空!")
//mesg_error.innerHTML="<font color='red'>email不能为空!</font>";
document.getElementById("eml").focus();
return false;
}


}

//检查密码
function checkpwd()
{
var mypwd=document.getElementById("pwd").value;
var pwd_error=document.getElementById("pwd_error");
if(mypwd=="")
{
//pwd_error.innerHTML="<font color='red'>密码不能为空!</font>";
alert("密码不能为空");
return false;
}
else if(mypwd.length>=6&&mypwd.length<=16)
{

pwd_error.innerHTML="<font color='red'>√</font>";
return true;
}
else
{
//pwd_error.innerHTML="<font color='red'>密码至少应为6位到16位!</font>";
alert("密码至少应为6位到16位!");
return false;
}
}

//检查确认密码
function checkpwdd()
{
var mypwdd=document.getElementById("pwdd").value;
var pwdd_error=document.getElementById("pwdd_error");

if(mypwdd=="")
{
//pwdd_error.innerHTML="<font color='red'>确认密码不能为空!</font>";
alert("确认密码不能为空!");
return false;
}

else if(document.getElementById("pwdd").value!=document.getElementById("pwd").value)
{
//pwdd_error.innerHTML="<font color='red'>确认密码与密码不一致!</font>";
alert("确认密码与密码不一致!");
return false;

}
else
{
pwdd_error.innerHTML="<font color='red'>√</font>";
return true;

}
}

//检查提交
function regist()
{
var email_err=document.getElementById('emai_error').innerHTML;
var pwd_err=document.getElementById('pwd_error').innerHTML;
var pwdd_err=document.getElementById('pwdd_error').innerHTML;

var yx=document.getElementById("eml").value;
var mima=document.getElementById("pwd").value;
var mmima=document.getElementById("pwdd").value;
//判断错误信息全部为空并且文本框全部不为空
if((yx!=""&&mima!=""&&mmima!="")&&(email_err==""&&pwd_err==""&&pwdd_err==""))
{
alert("恭喜您,注册成功!");
}
else
{
alert ("注册失败,请按页面的提示认真填写,谢谢!");

}

}

</script>
</head>

<body>
<div class="denglu">
<h1>注册模块</h1>

<div>
<span class="email">邮      箱:</span>
<input name="yx" class="email_txt" id="eml" type="text" width="100" onblur="checkemail()" />
<span id="emai_error" class="after_input">请输入邮箱,格式:xiaoniu@qq.com</span>
</div>
<div>
<span class="email">密      码:</span>
<input class="email_txt" type="password" width="100" id="pwd" onblur="checkpwd()" />
<span id="pwd_error" class="after_input">请输入密码</span>
</div>
<div>
<span class="email">确认密码:</span>
<input class="email_txt" type="password" id="pwdd" width="100" onblur="checkpwdd()" />
<span id="pwdd_error" class="after_input">再次输入密码</span>
</div>
<div id="btn">
<input class="sbmit" type="button" value="提交" onclick="regist()" />
</div>

</div>
</body>
</html>


...全文
307 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
scscms太阳光 2013-02-21
  • 打赏
  • 举报
回复
<!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>
    <link href="style/style2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="denglu">
    <h1>注册模块</h1>
    <div>
        <span class="email">邮      箱:</span>
        <input name="yx" class="email_txt" id="eml" type="text" width="100" />
        <span id="emai_error" class="after_input">请输入邮箱,格式:xiaoniu@qq.com</span>
    </div>
    <div>
        <span class="email">密      码:</span>
        <input class="email_txt" type="password" width="100" id="pwd" />
        <span id="pwd_error" class="after_input">请输入密码</span>
    </div>
    <div>
        <span class="email">确认密码:</span>
        <input class="email_txt" type="password" id="pwdd" width="100" />
        <span id="pwdd_error" class="after_input">再次输入密码</span>
    </div>
    <div id="btn">
        <input class="sbmit" type="button" value="提交" onclick="regist()" />
    </div>
</div>
<script type="text/javascript">
    function regist(){
        var eml=document.getElementById("eml");
        var pwd=document.getElementById("pwd");
        var pwdd=document.getElementById("pwdd");
        var reg=/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if(eml.value==""){
            alert("邮箱不能为空!");
            eml.focus();
        }else if(!reg.test(eml.value)){
            alert("邮箱不正确!");
            eml.focus();
        }else if(pwd.value==""){
            alert("密码不能为空!");
            pwd.focus();
        }else if(pwd.value.length<6||pwd.value.length>16){
            alert("密码至少应为6位到16位!");
            pwd.focus();
        }else if(pwd.value!=pwdd.value){
            alert("确认密码与密码不一致!");
            pwdd.focus();
        }else{
            alert("恭喜您,注册成功!");
            //应该发送ajax注册并返回结果判断
        }
    }
</script>
</body>
</html>
a4321262 2013-02-21
  • 打赏
  • 举报
回复
大侠们,能快点么,我好急啊,中午饭吃的都没心情了,折腾一上午了!
a4321262 2013-02-21
  • 打赏
  • 举报
回复
引用 2 楼 xzy21com 的回复:
XML/HTML code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht……
侠士,万分感谢,太谢谢你了!
泰坦小毛驴 2013-02-21
  • 打赏
  • 举报
回复
如果alert点击确定的事件无法处理的话,就别alert了,在后边加个div啥的,提示就行了,这样不会影响到你的焦点事件
泰坦小毛驴 2013-02-21
  • 打赏
  • 举报
回复
if(myemail.indexOf("@")==-1||myemail.indexOf(".")==-1||(myemail.indexOf("@")>myemail.indexOf("."))) { //mesg_error.innerHTML="<font color='red'></font>"; alert("email格式不正确!例:xiaoniu@qq.com"); document.getElementById("eml").focus(); return false; } 你这应该在alert点击确定事件中调用document.getElementById("eml").focus();才行的
飘飘哥 2013-02-21
  • 打赏
  • 举报
回复
引用 2 楼 xzy21com 的回复:
XML/HTML code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht……
athrunzero 2013-02-21
  • 打赏
  • 举报
回复
focus()

87,991

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧