开发者

jquery: focus and blur on the password input field

开发者 https://www.devze.com 2023-01-21 03:59 出处:网络
I only want to set the code to alert one when I am out of \'focus\' 开发者_如何转开发but the alert keeps piling up if I click on the input field and out of \'focus\' more than one?

I only want to set the code to alert one when I am out of 'focus' 开发者_如何转开发but the alert keeps piling up if I click on the input field and out of 'focus' more than one?

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());

                })
            });


        }
    });

});

the html

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

Have I coded something wrong? how can I fix it?

Thanks!


Because everytime it is on focus you are binding the next element to the blur event again and again.

Instead of:

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

Try this

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

I hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消