javascript如何让密码框显示默认值

如题所述

与设置文本框默认值的方法一样,设置密码框的value属性。

//普通设置
document.getElementById("pass_ipt").value="123456";

//jquery
$("#pass_ipt").val("123456");


//要显示密码框的内容,可以将密码框改为普通文本框
document.getElementById("pass_ipt").type="text";

温馨提示:内容为网友见解,仅供参考
第1个回答  2012-09-01
这样应该可以吧。应用到jquery
测试页面:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<input type="text" value="请输入用户名" id="username" style="width:150px;height:24px;"/>
<div id="pwd_warpper" style="width:150px;height:24px;">
<input type="text" value="请输入密码" id="_password" onfocus="showpassword();" style="width:150px;height:24px;"/>
</div>
<script type="text/javascript" src="jquery.js"></script> //你自己的jquery.js的文件名,需要导入
<script type="text/javascript" src="code.js"></script>

code.js
function showtext() {
if($("#password").val()=="") {
$("#pwd_warpper").html("<input type=\"text\" value=\"请输入密码\" id=\"_password\" onfocus=\"showpassword();\" style=\"width:150px;height:24px;\"/>");
}
}
function showpassword() {
$("#pwd_warpper").html("<input type=\"password\" value=\"\" id=\"password\" onblur=\"showtext();\" style=\"width:150px;height:24px;\"/>");
/**
这里为什么要用setTimeout,因为ie比较傻,刚创建完对象,你直接设置焦点
在ie下是不会响应的,你必须留出时间给ie缓冲下,所以加上了这个定时器
**/
setTimeout(function(){
$("#password").focus();
},20);
}
$(function(){
var usernameDefStr = $("#username").val();
$("#username").focus(function(){
if($(this).val()==usernameDefStr)
$(this).val("");
});
$("#username").blur(function(){
if($(this).val()=="")
$(this).val(usernameDefStr);
});

});本回答被网友采纳
第2个回答  2012-09-04
直接给密码框赋值就可以了,value=值就可以了,没你想的那么麻烦,根本不用js实现,直接从后台取出密码,前台显示,跟正常的text一样
第3个回答  2012-09-01
<input type="password" id="pwd" />
<script>
document.getElementById("pwd").value = "默认值";
</script>
第4个回答  2012-09-04
应该有value属性吧 给value属性赋值就可以了吧
相似回答