密码字段默认值

时间:2011-01-24 19:08:27

标签: javascript html

我如何使用HTML密码输入并设置可读的文本默认值,因为密码输入会将字符转换为点或星号?我可以用Javascript做到这一点吗?

7 个答案:

答案 0 :(得分:4)

因此,如果我理解正确,你希望它能够读到“密码”或其他常规字母,直到有人点击它,此时它会变成空白,那里输入的任何东西都会变成*?好的你想要的是这个:

头部:

<script type="text/javascript">
    function changefield(){
        document.getElementById("passwordbox").innerHTML = "<input id=\"password-field\" type=\"password\" name=\"password-field\" title=\"Password\" />";
        document.getElementById("password-field").focus();
    }
</script>

在体内:

<div id="passwordbox">
<input id="password-field" type="text" name="password-field" title="Password" onfocus="changefield();" value="password" />
</div>

答案 1 :(得分:2)

在头上:

    function Changetxt() {
        if ($('#pwd').val() == "Enter Your Password Here") {
            $('#pwd').val('');
            $('#pwd').css('color', 'black');
        }
    }

    function textboxtype() {
        if ($('#pwd').val().length < 1) {
            $('#pwd').val('Enter Your Password Here');
            $('#pwd').css('color', 'gray');
        }
    }

身体:

<input type="text" id="pwd" style="width: 180px; color: Gray;" onfocus="Changetxt();this.type='password'"
                            onblur="if(this.value==''){this.type='text';textboxtype();}" value="Enter Your Password Here" />

答案 2 :(得分:2)

仅使用HTML的更简单的选项:

添加此代码:

onfocus="this.value='', this.type='password'"

输入字段,并确保type最初设置为text

示例:

input onfocus="this.value='', this.type='password'" value="Password" type="text" title="Password" id="password_field" name="signin_password"

答案 3 :(得分:1)

试试这个javascript它一定会帮到你......

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Watermark Textbox for Password Textbox Using JavaScript</title>
<script language="javascript" type="text/javascript">
function WaterMark(objtxt, event) {
var defaultText = "Enter Username Here";
var defaultpwdText = "Enter Password Here";
// Condition to check textbox length and event type
if (objtxt.id == "txtUserName" || objtxt.id == "txtPwd") {
if (objtxt.value.length == 0 & event.type == "blur") {
//if condition true then setting text color and default text in textbox
if (objtxt.id == "txtUserName") {
objtxt.style.color = "Gray";
objtxt.value = defaultText;
}
if (objtxt.id == "txtPwd") {
document.getElementById("<%= txtTempPwd.ClientID %>").style.display = "block";
objtxt.style.display = "none";
}
}
}
// Condition to check textbox value and event type
if ((objtxt.value == defaultText || objtxt.value == defaultpwdText) & event.type == "focus") {
if (objtxt.id == "txtUserName") {
objtxt.style.color = "black";
objtxt.value = "";
}
if (objtxt.id == "txtTempPwd") {
objtxt.style.display = "none";
document.getElementById("<%= txtPwd.ClientID %>").style.display = "";
document.getElementById("<%= txtPwd.ClientID %>").focus();
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><b>UserName:</b></td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Text="Enter Username Here" Width="150px" ForeColor="Gray" onblur = "WaterMark(this, event);" onfocus = "WaterMark(this, event);" />
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td>
<asp:TextBox ID="txtTempPwd" Text="Enter Password Here" runat="server" onfocus="WaterMark(this, event);" Width="150px" ForeColor="Gray" />
<asp:TextBox ID="txtPwd" TextMode="Password" Text="Enter Password Here" runat="server" Width="150px" Style="display:none" onblur="WaterMark(this, event);"/>
</td>
</tr>
</table>
</form>
</body>
</html>

答案 4 :(得分:1)

您只需使用placeholder属性:

<input type="password" value="" placeholder="Insert your password..."/>

这样,一旦用户开始输入,建议就会自动消失。

答案 5 :(得分:-2)

<input type="password" name="x" id="x" value="some value" onclick="alert(this.value);" />

答案 6 :(得分:-2)

如果你这样做,字段文本就像用户名一样可读..为什么会出现这样的问题?

<input type="text" name="password" id="password" value="" />