admin管理员组文章数量:1306138
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 badges1 Answer
Reset to default 4Reading 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.
本文标签:
版权声明:本文标题:javascript - JQuery apply input mask to field onfocus and remove onblur so to avoid problems with placeholder text - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741814496a2398992.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论