好的我在输入字段中有默认文本"密码"当按下某个键时,我希望它改为输入类型"密码"。但是当我尝试这个时,输入没有注册我的第一次按键,但它在输入类型切换后记录所有按键。
function inputField(focus, inputValue, inputID){
// change inputID variable into pointer to the actual ID
var iD = document.getElementById(inputID);
// check if input has focus and handle default value changes, password field type changes, and font color changes.
if (focus == "on"){
if(iD.value == inputValue){
iD.setSelectionRange(0, 0);
iD.style.color = "#b2b2b2";
}
iD.onkeypress = function(){
if(iD.value == "password" || iD.value == "retype password"){
iD.type = "password";
}
if (iD.value != "" && iD.value == inputValue){
iD.value = "";
iD.style.color = "#000000";
}
}
}else if(focus == "off"){
if (iD.value == ""){
if(iD.type == "password"){
iD.type = "text";
}
iD.style.color = "#787878";
iD.value = inputValue;
}else if(iD.value == inputValue){
iD.style.color = "#787878"
}
}
}
<input
id = "registerPassword"
class = "loginSectionInput"
type = "text"
name = "rPassword"
value = "password"
onfocus = "inputField('on', 'password', this.id)"
onblur = "inputField('off', 'password', this.id)"
onchange = "formCheck('registerPassword')"
/>
答案 0 :(得分:0)
HTML5通过占位符属性为我们解决了这个问题。无需再次管理事件......请检查以下内容
<input type=password name="pwd" placeholder="Password">
答案 1 :(得分:0)
你可以用不同的方式做到,其中一些列在这里!希望我能帮到你
假设你有像 -
这样的字段<input type="text" id="mypswfield" name="mypswfield" />
方式1:
on onClick even,您可以使用Jquery替换它,
$('#mypswfield').replaceWith('<input type="password" id="mypswfield" />')
方式2:
$("[name=fieldname]").attr("type", "password");
(注意:这是jquery函数可能会在旧IE中发出如此小心)
方式3: 创建功能
function txtField()
{
if(document.getElementById('mypswfield').value=='')
{
document.getElementById('mypswfield').type='text';
document.getElementById('mypswfield').value='Password';
}
}
function txtPawd()
{
document.getElementById('mypswfield').type='password';
document.getElementById('mypswfield').value='';
}
HTML
<input id="mypswfield" onclick="txtField();" onblur="txtPawd();" name="mypswfield" type="text" value="Password">
答案 2 :(得分:0)
给你举个例子:
<html><head><script>function swithch(){
alert("myText is ");
var myText=document.getElementById("myId");
alert("myText is "+myText);
alert("myText is type "+myText.type);
alert("myText is value "+myText.value);
myText.type='text';
myText.value='56789';
}</script></head><body>
TT <input type="password" name="myText" id="myId" value="123456"/> <input type="button" name="switch" value="swithch" onclick="swithch()">
此代码已在google chrome上测试过。