触发器事件,当自动完成完成密码输入时

Trigger Event, when autocomplete completes the password input

本文关键字:密码 输入 事件 触发器      更新时间:2023-09-26

我有一个简单的登录表单,只要字段为空,我就想在字段中显示标题。问题是JS无法更改密码类型。

因此,当密码字段为空时,我进行了第二次输入,并与密码字段交换。

问题是,如果用户输入用户名,浏览器自动完成密码字段,则不会显示密码字段。我能做什么?

我的HTML:

<form name="login" method="post" action="/login" class="dialog form">
    <input type="submit" value="GO" />
    <div class="field">
        <input type="text" name="username" class="required" title="Username"/>
    </div>
    <div class="field">
        <input type="password" name="password" class="required"  />
        <input type="text" value="Password" class="password-title" />
    </div>
    <p class="clear">&nbsp;</p>
</form>

我的JS(需要jQuery):

       $("input[type=text]").each(function(){
           var input = $(this);
           
           if(input.val() == "") input.val(input.attr('title'));
           input.bind('click', function(){
               $(this).focus();
               if($(this).val() == $(this).attr('title')) $(this).val("");
           });
           input.bind('blur', function(){               
                if($(this).val() == "") $(this).val($(this).attr('title'));
           });
           
        });
        
        $("input[type=password]").each(function(){
            var input = $(this);
            var title = input.next('.password-title');
            
            if(input.val() == "" && title.length > 0){
                title.show();
                input.hide();
            }
            
            title.bind('click', function(){
                title.hide();
                input.show();
                input.focus();
            });
            
            input.bind('blur', function(){
                if($(this).val() == ""){
                    title.show();
                    input.hide();
                }
            });
            
            input.bind('DOMAutoComplete', function(){
                alert("dafuq"); //does not work (tested in Chromeium)
            });

我建议使用输入元素的占位符属性。点击此处了解更多信息。

input.bind('change', function(){
  title.hide();
  input.show();
  input.focus();
});