本帖最后由 Myprettygirl 于 2013-08-24 14:36:31 编辑

解决方案 »

  1.   

     $("#"+i).focus(); 改成
    setTimeout( function(){
     $("#"+i).focus();
    },50);
      

  2.   


    <!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" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <title>无标题文档</title>
    <script type="text/javascript">
    function checkUsername(ele){
    if(document.getElementById("username").value==""){
    document.getElementById("username").setAttribute('placeholder',"不能为空");
    //fix mozilla element focus bug
    setTimeout(function(){
    ele.focus();
    });
    }
    }
    function checkPsw(){
    if(document.getElementById("psw1").value != document.getElementById("psw2").value){
    document.getElementById("checkpassword").innerHTML="两次输入不一致";
    document.getElementById("psw2").focus();
    }
    }</script>
    </head><body>
    <div>
    <form name="f2">
    username:<input type="text" name="username" id="username" tabindex="1" onblur="checkUsername(this)"/><br/>
        password1:<input type="password" id="psw1" tabindex="2" /><br/>
        password2:<input type="password" name="psw2" id="psw2" tabindex="3" onkeypress="checkPsw()"/><br/>
        <label id="checkpassword"></label>
    </form>
    </div></body>
    </html>
      

  3.   

    不行,一直弹出 不能重复添加。
    按你的逻辑,输入框内容不改变成正确的就会一直弹,直到改成正确
    或者你可以改成文本提示
    settimeout不就是一直重复执行吗?这样写我连改的机会都没有。function w(i,v){
    var s = $.inArray(v,keyarr);
    if(s !=-1){
    alert('不能重复添加标签');
    setTimeout( function(){
     $("#"+i).focus();
    },50);
    return;
    }
    }
    这样就一直弹出。应该是弹出一次,然后聚焦就行了