如何让一个复选框控制两个密码字段?
<form>
<fieldset>
Password: <span id="capsalert">CAPS LOCK = on</span><br>
<input type="password" id="pwd">
<script type="text/javascript">
document.write(
'<input type="checkbox" name="masking" onclick="unmask(this.checked)"> ' +
'Show the password as I type'
);
</script>
<br>
Password2:<br>
<input type="password" id="pwd2">
</fieldset>
</form>
<script type="text/javascript">
function chkCaps(e) {
ev = (e ? e : window.event);
kc = (ev.which ? ev.which : (ev.keyCode ? ev.keyCode : false));
sk = (ev.shiftKey ? ev.shiftKey : (ev.modifiers ? !!(ev.modifiers & 4) : false));
if(
(kc >= 97 && kc <= 122 && sk) ||
(kc >= 65 && kc <= 90 && !sk)
) {
document.getElementById('capsalert').style.display = 'inline';
}
else {
document.getElementById('capsalert').style.display = 'none';
}//end if
}//end function
function unmask(truefalse) {
oldElem = document.getElementById('pwd');
elem = document.createElement('input');
elem.setAttribute('type', (truefalse == true ? 'text' : 'password'));
elem.setAttribute('value', document.getElementById('pwd').value);
elem.id = 'pwd';
oldElem.parentNode.replaceChild(elem,oldElem);
document.getElementById('pwd').onkeypress = function(e) { chkCaps(e); };
}//end function
document.getElementById('pwd').onkeypress = function(e) { chkCaps(e); };
</script>
我以稍微复杂的形式使用上面的代码。
我的表单上有两个单独的“密码”字段。使用当前代码,我可以让第一个密码字段显示选中复选框时键入的字符。
此外,如果用户在启用大写锁定的情况下键入,代码还会通知用户。
我希望两个密码字段表现出相同的行为,而不仅仅是第一个字段。不幸的是,我不知道如何实现这一点。
感谢您的帮助。
编辑:
使用以下代码可能更容易找到简单的解决方案。我愿意使用其中任何一个。
<script>
function changeType()
{
document.myform.pass.type=(document.myform.option.value=(document.myform.option.value==1)?'-1':'1')=='1'?'password':'text';
}
</script>
<body>
<form name="myform">
<input type="password" name="pass" />
<input type="password" name="pass2" />
<input type="checkbox" name="option" value='1' onchange="changeType()" />
</form>
</body>
<form>
<fieldset>
Password: <span id="capsalert">CAPS LOCK = on</span><br>
<input type="password" id="pwd">
<script type="text/javascript">
document.write(
'<input type="checkbox" name="masking" onclick="unmask(this.checked)"> ' +
'Show the password as I type'
);
</script>
<br>
Password2:<br>
<input type="password" id="pwd2">
</fieldset>
</form>
<script type="text/javascript">
function chkCaps(e) {
ev = (e ? e : window.event);
kc = (ev.which ? ev.which : (ev.keyCode ? ev.keyCode : false));
sk = (ev.shiftKey ? ev.shiftKey : (ev.modifiers ? !!(ev.modifiers & 4) : false));
if(
(kc >= 97 && kc <= 122 && sk) ||
(kc >= 65 && kc <= 90 && !sk)
) {
document.getElementById('capsalert').style.display = 'inline';
}
else {
document.getElementById('capsalert').style.display = 'none';
}//end if
}//end function
function unmask(truefalse) {
oldElem = document.getElementById('pwd');
elem = document.createElement('input');
elem.setAttribute('type', (truefalse == true ? 'text' : 'password'));
elem.setAttribute('value', document.getElementById('pwd').value);
elem.id = 'pwd';
oldElem.parentNode.replaceChild(elem,oldElem);
document.getElementById('pwd').onkeypress = function(e) { chkCaps(e); };
}//end function
document.getElementById('pwd').onkeypress = function(e) { chkCaps(e); };
</script>
I'm using the above code in a slightly more complex form.
I have two separate "password" fields on the form. With the current code I can have the first password field show the characters as they are typed when the checkbox is ticked.
Also, the code notifies the user if they are typing with CAPS Lock enabled.
I would like to have both password fields exhibiting the same behavior rather than the first field only. Unfortunately, I do not know how to make that happen.
Thanks for the help.
EDIT:
A simple solution might be easier to find with the following code. I'm willing to use either one.
<script>
function changeType()
{
document.myform.pass.type=(document.myform.option.value=(document.myform.option.value==1)?'-1':'1')=='1'?'password':'text';
}
</script>
<body>
<form name="myform">
<input type="password" name="pass" />
<input type="password" name="pass2" />
<input type="checkbox" name="option" value='1' onchange="changeType()" />
</form>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不在
unmask()
函数中更改密码输入的type
属性呢?这样您就可以更轻松地管理多个字段。检查是否必须将输入
转换为文本
或密码
后,执行以下操作:Why don't you just change the
type
attribute of the password inputs in yourunmask()
function? That way it will be easier for you to manage more than 1 field. After checking whether you have to turn theinput
into atext
one or apassword
one, do this:我建议稍微改变一下你的方法。
您可以修改客户端而不影响服务器代码。例如,我会尝试使用 2 个同名的输入框。如果您有一个框可见,而另一个框设置了“禁用”HTML 属性,则可见框将始终是唯一提交的框。
这样,您的服务器端代码只需在单个名称下查找 1 个输入。
I suggest changing your approach a bit.
You could modify the client with no impact to the server code. For example, I would try using 2 input boxes with the same name. If you ever have one box visible while the other one has the 'disabled' HTML attribute set, the visible box will always be the only one submitted.
This way, your server-side code would only have to look for 1 input, under a single name.
jQuery 大写锁定 测试/功能?
将两个字段设置为密码类别并且:
jQuery caps lock test/function?
set both fields to class of password and:
试试这个:
它的作用是通过对数组中的每个元素重复它
new Array('pwd', 'pwd2')
。Try this:
What this does is make it so your code works on
pwd
andpwd2
by repeating it for each element in the arraynew Array('pwd', 'pwd2')
.大多数答案都不起作用。我没有尝试其中一两个,因为它们意味着对我的代码的更改超出了我的预期。我最终找到了另一种泄露密码的方法:“将密码显示为文本”控件
我根据我的多个密码字段场景调整了该方法:
Most of the answers didn't work. One or two I left untried because they meant greater changes to my code than I wanted. I ended up finding yet another method to reveal the passwords: "Show password as text" control
I adapted the method there to my multiple password fields scenario: