开发者

Jquery default value in password field

开发者 https://www.devze.com 2023-03-03 20:00 出处:网络
I am using this Jquery plugin for populating inputs with text that disappears on click. It isn\'t ideal for password fields because everything shows up as dots. What would be a good way to make a defa

I am using this Jquery plugin for populating inputs with text that disappears on click. It isn't ideal for password fields because everything shows up as dots. What would be a good way to make a default text visible in password fields before you start t开发者_如何学JAVAyping?


Through JS, my answer would be the same as @ultimatebuster's. However, the whole JS route is hacky, now that alternatives started appearing. Many modern browsers now support this thing directly through HTML5:

<input type="password" name="password" placeholder="Enter password"/>

(Many modern browsers = every major one except the Internet Explorer. I refuse to code for it; if you have to have the same thing in IE as well, you'll have to go the hacky route.)


You could set the type of the input field as password. However, set it to normal via javascript upon page load (this way you can fallback easily if the user doesn't have JS). Once it receives a click, set the type of the input field back to a password.


Like suggested, you can swap the inputs, but it doesn't work in IE. IE won't allow it since it may be some sort of security hole.

I used to use this:

/* From: http://grzegorz.frydrychowicz.net/jquery_toggleformtext/
   Modified to swap password textbox type so watermark can be read */
$(document).ready(function() {
    if (!jQuery.browser.msie) {
        $("input:password").each(function() {
            if (this.value == '') {
                this.type = "text";
            }
            $(this).focus(function() {
                this.type = "password";
            });
            $(this).blur(function() {
                if (this.value == '') {
                    this.type = "text";
                }
            });
        });
    }
    $("input:text, textarea, input:password").each(function() {
        if (this.value == '') {
            $(this).addClass("watermark");
            this.value = this.title;
        }
    });
    $("input:text, textarea, input:password").focus(function() {
        $(this).removeClass("watermark");
        if (this.value == this.title) {
            this.value = '';
        }
    });
    $("input:text, textarea, input:password").blur(function() {
        if (this.value == '') {
            $(this).addClass("watermark");
            this.value = this.title;
        }
    });
    $("input:image, input:button, input:submit").click(function() {
        $(this.form.elements).each(function() {
            if (this.type == 'text' || this.type == 'textarea' || this.type == 'password') {
                if (this.value == this.title && this.title != '') {
                    this.value = '';
                }
            }
        });
    });
});

I finally just gave up an went with the normal password input behavior. I found the above input swapping to be a bit quirky, but you can give it a shot.

0

精彩评论

暂无评论...
验证码 换一张
取 消