复选框选中的时候显示密码password改为text

来源:互联网 发布:淘宝成交价 编辑:程序博客网 时间:2024/05/16 00:44

纠结了半天,就为了实现把密码显示出来,而不是用*号代替

我们一般用的<input type="password" id="account_pwd" name="account_pwd" value="<%=account.getAccount_pwd()%>" />默认显示出来的是*****号代替的,在后面加个复选框,当复选框被选中的时候让密码显示出来,也就是password改为text


很容易进入一个错误的思想,就是想着去获取这个标签,然后改变它的类型type="text"

要是这样想的话你就打错特错了【(*^__^*) 嘻嘻,我之前就是这么想的、o(︶︿︶)o 唉、笨】


type属性在jsp里面是只读的,你根本不能改变type的值,但是在一般的html中可以使用type=“text”来实现效果


说了这么多来看看我是怎么实现的【哈哈,有点啰嗦】

html:


<input type="hidden"  name="value" value="<%=account.getAccount_pwd()%>" />
<span id="pwdTd" style="width: 100px;">
<input type="password" id="account_pwd" name="account_pwd" value="<%=account.getAccount_pwd()%>" />
</span>
<input type="checkbox" id="show" onclick="showPwd()"/>显示密码



js:

function showPwd(){
var show=document.getElementById("show");
var value=document.getElementById("value").value;//获取隐藏表单域中存储的从数据库中读取出来的值
if(show.checked==true){
document.getElementById("pwdTd").innerHTML="<input type='text' id='account_pwd' name='account_pwd' value='"+value+"' />";
}else{
document.getElementById("pwdTd").innerHTML="<input type='password' id='account_pwd' name='account_pwd' value='"+value+"' />";
}
}