最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery apply input mask to field onfocus and remove onblur so to avoid problems with placeholder text - Stack Overf

programmeradmin0浏览0评论

I have a date of birth text field:

<input type="text" id="dob" name="dob" placeholder="Date of Birth" />

I'm using a jQuery plugin (Charl van Niekerk's Placeholder text) to make sure that older browsers see the placeholder text. All good so far.

When my date of birth field is clicked on, I want to switch from the placeholder text to an input mask. For that I'm using another plugin (/).

However I only want to activate the input mask onfocus, and remove it onblur, otherwise it stops the placeholder text from being shown.

I need something like:

$(document).ready(function() {    
    $('#dob').focus(function() {  
        $.mask.definitions['~']='[+-]';
        $('#dob').mask('99/99/9999');  
    });
    $('#dob').blur(function() {
        // Something here to unmask?   
    });  
});

This code doesn't work at the moment. Any ideas?

I have a date of birth text field:

<input type="text" id="dob" name="dob" placeholder="Date of Birth" />

I'm using a jQuery plugin (Charl van Niekerk's Placeholder text) to make sure that older browsers see the placeholder text. All good so far.

When my date of birth field is clicked on, I want to switch from the placeholder text to an input mask. For that I'm using another plugin (http://digitalbush./projects/masked-input-plugin/).

However I only want to activate the input mask onfocus, and remove it onblur, otherwise it stops the placeholder text from being shown.

I need something like:

$(document).ready(function() {    
    $('#dob').focus(function() {  
        $.mask.definitions['~']='[+-]';
        $('#dob').mask('99/99/9999');  
    });
    $('#dob').blur(function() {
        // Something here to unmask?   
    });  
});

This code doesn't work at the moment. Any ideas?

Share Improve this question edited Jan 27, 2011 at 13:10 Paul asked Jan 27, 2011 at 12:50 PaulPaul 6714 gold badges12 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Reading the plugin code, it should be possible to do this (untested):

(function($) {
  $(document).ready(function() {
    $('#dob').focus(function() {
      $.mask.definitions['~']='[+-]';
      $(this).mask('99/99/9999');
    }).blur(function() {
      $(this).unmask();
    });
  });
})(jQuery);

Note that I added (function($){})(jQuery) - I believe it is good practice to wrap all your code this way.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论