Jquery 如果密码字段为空
我正在尝试将可见文本添加到显示“输入密码”的密码字段中,单击后文本将被清除,您可以键入点。我有两个输入,一个是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎期望在调用
focus()
之后出现某种“暂停”,并且仅当最终用户以某种方式完成输入密码时才会执行 JS 代码的剩余部分。这不是真的。该函数一次性全部执行完毕。
您需要将以下部分移动
到另一个独立函数中:
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
into another standalone function:
您的意思是:
如果您想为旧版浏览器实现此功能,则必须将检查移至 onBlur 事件。在 jQuery 中,这看起来像:
Do you mean:
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: