Jquery 如果密码字段为空

发布于 2024-11-06 18:05:07 字数 1322 浏览 0 评论 0原文

我正在尝试将可见文本添加到显示“输入密码”的密码字段中,单击后文本将被清除,您可以键入点。我有两个输入,一个是 type=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 输入将显示。所以它不尊重 ifempty 语句。由于某种原因,即使我输入了密码,它也会将输入视为空。

我在这里做错了什么?

I'm trying to add visible text to a password field that says "Enter a password" and on click the text is cleared and you type dots. I have two inputs one of type=text and another password. The password input is hidden from the start but appears after you click on the input with the "Enter a password" instruction.

It does this http://mudge.github.com/jquery_example/ but I'm trying to make it for password fields.

HTML

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

The 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
                    });                     
                }
        });

What doesn't work:

When you fill the password input (with dots appearing) and click out the input is hidden and the #password_instruction input shows. So it's not respecting the if empty statement. For some reason it treats the input as empty even if I typed a password in.

What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

客…行舟 2024-11-13 18:05:08

您似乎期望在调用 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
    }
});

You seem to be expecting that there's some kind of "pause" after you call focus() and that the remnant of the JS code will be executed only when the enduser is finished typing the password somehow.

This is not true. The function is been executed fully in one go.

You need to move the following piece

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

into another standalone function:

$('#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
    }
});
ぽ尐不点ル 2024-11-13 18:05:08

您的意思是:

<input type="password" placeholder="enter password">

如果您想为旧版浏览器实现此功能,则必须将检查移至 onBlur 事件。在 jQuery 中,这看起来像:

$('#password').blur(function() {
    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
        });                     
    }
});

Do you mean:

<input type="password" placeholder="enter password">

If you are wanting to implement this for legacy browsers, you'll have to move the check to the onBlur event. In jQuery this looks like:

$('#password').blur(function() {
    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
        });                     
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文