Cyb*_*kie 7 html javascript forms jquery input
我正在尝试将可见文本添加到"输入密码"的密码字段中,然后单击文本清除并键入点.我有两个输入类型= text和另一个密码.密码输入在开始时隐藏,但在使用"输入密码"指令单击输入后出现.
它这样做http://mudge.github.com/jquery_example/但我正在尝试将其用于密码字段.
HTML
<input type="text" id="password_instructions" />
<input type="password" id="password" />
Run Code Online (Sandbox Code Playgroud)
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
});
}
});
Run Code Online (Sandbox Code Playgroud)
什么行不通:
当您填写密码输入(出现点)并单击时,隐藏输入并显示#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
});
}
Run Code Online (Sandbox Code Playgroud)
进入另一个独立功能:
$('#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
}
});
Run Code Online (Sandbox Code Playgroud)