#notepad_and_pen
可以通过单行来施加限制并允许删除。
此页面提供了一种方法来检测是否原生支持占位符(从而避免了对 Modernizr 的需求)。
这个页面提供了一种非本地处理占位符的合理方法。
这是组合代码:
$(function() {
//Impose limits on #notepad_and_pen
$("#notepad_and_pen").on('change', function() {
this.value = (this.value === '') ? '' : Math.max(400, Math.min(999999, Number(this.value)));
//Use the following line to reject anything that's zero, blank or not a number, before applying the range limits.
//this.value = (!Number(this.value)) ? '' : Math.max(400, Math.min(999999, Number(this.value)));
});
//With reference to http://diveintohtml5.info/detect.html
function supports_input_placeholder() {
var i = document.createElement('input');
return 'placeholder' in i;
}
//With reference to http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
// 1. Handle placeholders (in non-HTML5 compliant browsers).
if(!supports_input_placeholder()) {
$('[placeholder]').focus(function () {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function () {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
//2. Prevent placeholder values being submitted to form's action script
$('[placeholder]').eq(0).closest('form').submit(function () {
$(this).find('[placeholder]').each(function () {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
});
});
}
});