Jquery 密码字段中的默认值



我正在使用这个Jquery插件来填充输入,点击后消失的文本。它不适合密码字段,因为所有内容都显示为点。在开始键入之前,使默认文本在密码字段中可见的好方法是什么?

通过 JS,我的答案将与@ultimatebuster相同。然而,整个JS路由都是黑客,现在替代品开始出现。许多现代浏览器现在直接通过HTML5支持这个东西:

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

(许多现代浏览器=除Internet Explorer之外的每个主要浏览器。我拒绝为它编码;如果你也必须在IE中拥有同样的东西,你将不得不走黑客路线。

您可以将输入字段的类型设置为密码。但是,在页面加载时通过javascript将其设置为正常(这样,如果用户没有JS,则可以轻松回退)。收到单击后,将输入字段的类型设置回密码。

按照建议,您可以交换输入,但它在IE中不起作用。IE不允许它,因为它可能是某种安全漏洞。

我曾经使用过这个:

/* 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 = '';
                }
            }
        });
    });
});

我终于放弃了正常的密码输入行为。我发现上面的输入交换有点古怪,但你可以试一试。

最新更新