【问题标题】:Placeholder for form elements using Jquery使用 Jquery 的表单元素的占位符
【发布时间】:2011-09-13 20:11:24
【问题描述】:

首先我想提一下,这个问题的目的不是询问它是如何完成的,而是要了解为什么我这样做的方式有效。

我想为表单元素创建占位符文本,在玩 jquery 时我想出了以下代码:

function placehold(id,placeholder) // id of element and placeholder text
{
    $("#"+id).val(placeholder); //initialize form field

    $("#"+id).focusin(function() //on focus
    {
        if ($(this).val()==(placeholder))
        {
            $(this).val(null); 
            /*if input's value is placeholder then empty
              input and wait for value */
        }        
    });


    $("#"+id).focusout(function() //on lost focus
    {
        if ($(this).val()==(""))
        {
            $(this).val(placeholder); //reset val to placeholder text if empty
        }        
    });
}

即使我在多个字段上调用该函数,这也会起作用,例如,

placehold('uname','Please enter a username');
placehold('pword','Please enter a password);
placehold('email','Please enter an email address');

在上述情况下,它将按预期在所有 3 个文本框中工作,这就是我的问题: 运行时不同的占位符存储在哪里?是为其绑定的每个字段保留的函数实例吗?如果是,从长远来看,这会对性能产生影响吗?

感谢您的时间和帮助:)

【问题讨论】:

    标签: javascript jquery forms placeholder


    【解决方案1】:

    是为其绑定的每个字段保留的函数实例吗?

    是的。每次调用placehold(),都会创建新的事件处理函数。 idplaceholder 的值作为您创建的闭包的一部分保留。

    如果是,从长远来看,这会对性能产生任何影响吗?

    按照您的做法,您调用placehold() 的每个元素都会产生一些开销。不过,我不会担心这一点,它确实可以忽略不计。

    重用事件处理函数会更好:

    function placeholderEmpty() { 
        var $this = $(this);
        if ( $this.val() == $this.data("placeholder") ) {
            $this.val(""); 
        }        
    }
    function placeholderDefault() {
        var $this = $(this);
        if ( $this.val() == "" ) {
            $this.val( $this.data("placeholder") ); 
        }        
    }
    
    function placehold(id,placeholder) {
      $("#"+id).data("placeholder", placeholder) 
      .focusin(placeholderEmpty)
      .focusout(placeholderDefault)
      .trigger("focusout");
    } 
    

    下一步将是一个插件(注意使用另一个闭包):

    $.fn.extend({
      placehold: function () {
        var placeholderEmpty   = function () { /* ... */ };
        var placeholderDefault = function () { /* ... */ };
    
        return function(placeholder) {
          this.data("placeholder", placeholder) 
          .focusin(placeholderEmpty)
          .focusout(placeholderDefault)
          .trigger("focusout");
          return this; 
        }
      }();
    });
    

    用作

    $('#uname').placehold('Please enter a username');
    $('#pword').placehold('Please enter a password');
    $('#email').placehold('Please enter an email address');
    $('.manyTextFields').placehold('All get the same text & behavior.');
    

    【讨论】:

    • 嗯,我在想一定有比我做的更有效的方法。感谢您的回答和代码建议^^
    • @kyushiro 另见插件建议!
    • @tomalak 我没有真正考虑过插件选项,因为我还没有探索过 JQuery 开发的这个方面,但是你发布的代码让它看起来非常简单。再次感谢您的提醒:)
    • 出于好奇,在使用此功能时放置一个占位符会使占位符本身成为一个值吗?如果是这样,您是否必须在那时修改验证?
    • @Brad:是的,当然它会成为一个价值。解决方案是通过每个表单字段上的提交回调删除默认值,或者忽略服务器端的默认值。
    猜你喜欢
    • 2012-10-21
    • 2019-08-17
    • 2013-11-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多