JQuery: фокус и размытие на поле ввода пароля - PullRequest
1 голос
/ 22 октября 2010

Я хочу установить код для оповещения, только когда я не в «фокусе», но предупреждение продолжает накапливаться, если я нажимаю на поле ввода и не «фокусируюсь» более чем на один?

http://nano -visions.net / dump2 / фокус /

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

                })
            });


        }
    });

});

HTML

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

Я что-то не так кодировал? как я могу это исправить?

Спасибо!

1 Ответ

1 голос
/ 22 октября 2010

Поскольку каждый раз, когда он находится в фокусе, вы снова и снова привязываете следующий элемент к событию размытия.

Вместо:

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

Попробуйте это

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

Надеюсь, это поможет!

...