代码之家  ›  专栏  ›  技术社区  ›  Run

jquery:对密码输入字段进行焦点和模糊处理

  •  1
  • Run  · 技术社区  · 15 年前

    我只想将代码设置为在“焦点”不在时提醒一个,但如果我单击输入字段并不止一个“焦点”不在,则警告会不断堆积?

    http://nano-visions.net/dump2/focus/

    $(document).ready(function(){
    
      $(function() {
        $('#test-form-1 *[title]').inputHint();
    
      });
    
      $(".input-password").each(function(){
    
            if($(this).length > 0)
            {
                /* create the password input next to the current input and hide the password input */
                var val_name = $(this).attr('name');
                $(this).after("<input name='"+val_name+"' type='password' value='' />");
                $(this).next().hide();
    
                /* on focus */
                $(this).focus(function () {
    
                    /* hide the input and display the next input and put focus on it */
                    $(this).hide();
                    $(this).next().show().focus();
    
                    //alert($(this).parent().html());
                    //var keep_html = $(this).parent().html()
    
                    /* on blur */
                    $(this).next().blur(function(){
    
                        /* if the password input is empty */
                        if($(this).val() == '')
                        {
                            $(this).hide();
                            $(this).prev().show();
                            $('form *[title]').inputHint();
                        }
                        alert($(this).parent().html());
    
                    })
                });
    
    
            }
        });
    
    });
    

    <form method="post" id="test-form-1">
      <div><input value="" class="hint input-password" title="Password" name="password" type="text"></div>
    </form>
    

    我是不是编码有问题?我怎样才能修好它?

    谢谢!

    1 回复  |  直到 15 年前
        1
  •  1
  •   Mouhannad    15 年前

    因为每次聚焦时,都会一次又一次地将下一个元素绑定到模糊事件。

    而不是:

    $(this).focus(function () {
      // code
      $(this).next().blur(function(){
        //code
      })
    });
    

    试试这个

    $(this).focus(function () {
      // code
    }).next().blur(function(){
      //code
    });
    

    我希望这有帮助!