【问题标题】:How can I use Jquery's Coolinput to show the hint "Enter your password" in a password field, but then use stars to hide the input when user types?如何使用 Jquery 的 Coolinput 在密码字段中显示提示“输入您的密码”,然后在用户键入时使用星号隐藏输入?
【发布时间】:2009-06-10 16:43:46
【问题描述】:

非常不言自明...

【问题讨论】:

    标签: jquery input hide


    【解决方案1】:

    将您的密码文本框设为“文本”类型:

    <input type="text" alt="Enter Password" name="PWD" />
    

    然后使用以下脚本:

    $(function() {
       $("input[name=PWD]")
          .focus(function() { $(this).attr("type","password"); })
          .blur(function() { 
              if ($(this).val()) // check if you entered something
                 $(this).attr("type","text"); 
           })
          .coolinput();
    });
    

    它的作用是这样的:当接收到焦点时,文本框变为密码框,当焦点丢失时,它返回到普通文本框(因此提示文本是可读的),除非当然是在文本框中输入。

    我实际上并没有对此进行测试,但如果它不能正常工作,至少它会为你指明方向。

    编辑:

    您似乎无法使用 javascript 更改文本框的类型,所以这里有一个解决方法:

     <input id="PWD1" name="PWD1" value="Enter password"/>
     <input id="PWD2" name="PWD2" type="password" style="display:none" />
    

    Javascript:

    $(function() {
       $("#PWD1").focus(function() {  $("#PWD2").show().focus(); $("#PWD1").hide(); });
       $("#PWD2").blur(function() {
    
         if ($(this).val().length == 0) {
                 $("#PWD1").show();
                 $("#PWD2").hide();
         }
       });
    
    });
    

    你可以在这里看到它的实际效果:http://jsbin.com/iniza

    【讨论】:

    • 很遗憾无法更改类型属性
    【解决方案2】:

    也许你可以使用 jquery impromptu 插件,点击链接获得详细解释......简而言之,你将使用表单的标准输入类型密码。

    Impromptu

    类似这样的:

    var txt = 'Enter your password:<br />
          <input type="password" id="jPassword"
          name="jPassword" value="" />';
    
    function mycallbackform(v,m,f){
        if (v){
          $.prompt('your password is of '+ f.jPassword.length + 'characters');
        } 
    }
    
    $.prompt(txt,{
          callback: mycallbackform,
          buttons: { Accept: true, Cancel: false }
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2011-10-17
    • 2012-08-09
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多