Label for password field

2019-02-10 04:34发布

<input type="text" value="useraname" />
<input type="password" value="password" />

I'm using jQuery to make the inline labels disappear on click/focus. Password shows bulls as usual, but I wonder if its possible somehow to show "Password" label as text (instead of ••••) inside the password field?

Edited to add: I want the user-typed password to be hidden ofcourse!.

Thanks

7条回答
女痞
2楼-- · 2019-02-10 05:10

why all that long script for? that jus a simple task which can be done with a half line of code :

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

cheers

查看更多
做个烂人
3楼-- · 2019-02-10 05:17

I think you'd need to dynamically overlay an <input type="text"> on top of the password field to do this.

查看更多
三岁会撩人
4楼-- · 2019-02-10 05:18

There are plugins for that. See labelify for example

查看更多
够拽才男人
5楼-- · 2019-02-10 05:18

If you make the input type="textbox" you'll be able to see the password.

查看更多
仙女界的扛把子
6楼-- · 2019-02-10 05:23

My solution is close to what Dan provided above.

<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>

An alternative here would be to omit the "label" parameter, and just use the "title" value, which is basically what labelify.js does. You could add in the part of labelify that just applies these event handlers to all inputs of type text if you wanted. Or download labelify and add the below code where needed:

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

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

The only advantage my snippet has over labelify is that it includes the answer to your original question:

I wonder if its possible somehow to show "Password" label as text (instead of ••••) inside the password field?

查看更多
干净又极端
7楼-- · 2019-02-10 05:26

Check out the code below. Just append addPassClear("Password") to any input element that you want this functionality for.

$(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";
    }
  });
};
};
查看更多
登录 后发表回答