javascript – 如果密码字段为空则为Jquery

我正在尝试将可见文本添加到“输入密码”的密码字段中,然后单击文本清除并键入点.我有两个输入类型= text和另一个密码.密码输入在开始时隐藏,但在使用“输入密码”指令单击输入后出现.

它执行此操作http://mudge.github.com/jquery_example/,但我正在尝试将其用于密码字段.

HTML

<input type="text" id="password_instructions" />
<input type="password" id="password" />

Jquery

var $password = $('#password');
        $password.hide(); //hide input with type=password

        $("#password_instructions").click(function() {
                $( this ).hide();
                $('#password').show();
                $('#password').focus();

                if ($password.val().length === 0) { //if password field is empty            
                    $password.focusout(function() { //when clicking outside empty password input
                        $( this ).hide();
                        $('#password_default_value').show();
                        $('#password_instructions').default_value('Enter a password'); //will clear on click
                    });                     
                }
        });

什么行不通:

当您填写密码输入(出现点)并单击时,隐藏输入并显示#password_instruction输入.所以它不尊重if empty语句.出于某种原因,即使我输入了密码,它也会将输入视为空.

在这做错了什么?

解决方法

调用focus()之后,您似乎期待某种“暂停”,并且只有当最终用户以某种方式输入密码时才会执行JS代码的残余.

这不是真的.该功能一次完全执行.

您需要移动以下部分

if ($password.val().length === 0) { //if password field is empty            
            $password.focusout(function() { //when clicking outside password input
                $(this).hide();
                $('#password_default_value').show();
                $('#password_instructions').default_value('Enter a password'); //will clear on click
            });                     
        }

进入另一个独立功能

$('#password').focusout(function() {
    if ($(this).val().length === 0) { //if password field is empty            
        $(this).hide();
        $('#password_default_value').show();
        $('#password_instructions').default_value('Enter a password'); //will clear on click
    }
});

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...