2009-12-10 62 views
5
<input type="text" value="useraname" /> 
<input type="password" value="password" /> 

我正在使用jQuery使内联标签在点击/焦点上消失。密码像往常一样显示公牛,但我想知道它是否可能以某种方式在密码字段内显示“密码”标签作为文本(而不是••••)?密码字段标签

编辑添加:我想用户键入的密码被隐藏的课程!

谢谢

回答

6

有插件。例如,请参见labelify

+0

除非我误认为插件文档没有提到密码。我只是迅速将其中一种字段类型更改为“密码”,所有字符都变成了圆点。这是否与OP请求的密码字段一起工作? – megaSteve4 2012-03-13 22:41:05

+0

同样的故事在这里。它仅适用于文本字段,不适用于密码字段。 – 2012-03-21 19:47:26

0

如果您使输入类型=“文本框”,您将能够看到密码。

+0

Ofcourse,但然后密码也将可见。我只需要标签是文本。 – 3zzy 2009-12-10 05:22:16

+0

即时通讯不知道你是什么意思。 – Galen 2009-12-10 05:29:56

+4

只要箱子获得焦点,请将其更改为type =“密码”。据推测,你已经有了代码清除箱子聚焦时的值,所以只需将它添加到那里。 虽然插件路线可能会更好。 – McPherrinM 2009-12-10 05:31:35

0

我认为您需要在密码字段顶部动态覆盖< input type =“text”>才能执行此操作。

1

查看下面的代码。只需将addPassClear("Password")附加到您希望使用此功能的任何输入元素即可。

$(function() { 
$.fn.addPassClear = 
function(text) 
{ 
    return $(this).each(
    function() 
    { 
    $(this).focus(function(event){$(this).passFocusize(text); $(this).select(); }); 
    $(this).blur(function(event){$(this).passBlurize(text); }); 
    }); 
} 
$.fn.passFocusize = 
function(text) 
{ 
    return $(this).each(
    function() 
    { 
    if ($(this).val()==text && $(this).attr("type")=="text") 
    { 
     $(this).val(""); 
     this.type="password"; 
    } 
    }); 
}; 
$.fn.passBlurize = 
function(text) 
{ 
    return $(this).each(
    function() 
    { 
    if ($(this).val()=="") 
    { 
     $(this).val(text); 
     this.type="text"; 
    } 
    }); 
}; 
}; 
4

为什么所有这些长脚本的?这绝对一个简单的任务,可以用代码半行来完成:

<input type='text' onclick="this.type='password',value=''" class='watever' value='password'..etc 

欢呼

+1

喜欢这个,但我认为'onfocus'比'onclick'更好,因为当用户进入该领域时它也会触发...... – neokio 2011-11-22 09:25:45

0

我的解决办法是紧挨什么丹上面提供。这里

<script language="javascript" type="text/javascript"> 
    function SwapLabel(id, label, alttype) { 
     $(id).focus(function() { 
      if (this.value == label) { 
       this.value = ""; 
       if (this.type == 'text' && label == 'Password') { 
        this.type = alttype; 
       } 
      } 
     }); 
     $(id).blur(function() { 
      if (this.value == "") { 
       this.value = label; 
       if (this.type == alttype && label == 'Password') { 
        this.type = 'text'; 
       } 
      } 
     }); 
    } 
    $(document).ready(function() { 
     SwapLabel('#UserName', 'Username', ''); 
     SwapLabel('#Password', 'Password', 'password'); 
    }); 
</script> 

另一种方法是省略了“标签”参数,只需使用“标题”值,这是labelify.js做基本上是什么。如果需要,可以在labelify的部分中添加仅将这些事件处理程序应用于文本类型的所有输入。或labelify下载并添加下面的代码在需要的地方:

  if (this.type == 'text' && label == 'Password') { 
       this.type = alttype; 
      } 

      if (this.type == 'text' && label == 'Password') { 
       this.type = alttype; 
      } 

唯一的好处我片断拥有labelify是它包括回答你原来的问题:

我不知道它可能以某种方式在密码字段中显示“密码”标签为文本(而不是••••)?