jQuery启用Tab键输入



我使用下面的jQuery来格式化输入字段中的电话号码,效果很好!除了一件事,它禁用了除数字之外的所有输入键,这对电话号码来说是有意义的。

问题是Tab键也被禁用了,我喜欢在填写表格进入下一个字段时使用它。如何启用Tab键输入?

(function ($) {
$.fn.usPhoneFormat = function (options) {
var params = $.extend({
format: 'xxx-xxx-xxxx',
international: false,
}, options);
if (params.format === 'xxx-xxx-xxxx') {
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent && e.originalEvent.clipboardData.getData('Text');
inputValue = inputValue.replace(/D/g, '');
if (!$.isNumeric(inputValue)) {
return false;
} else {
if (inputValue.length > 9) {
inputValue = String(inputValue.replace(/(d{3})(d{3})(d{4})/, "$1-$2-$3"));
} else {
inputValue = String(inputValue.replace(/(d{3})(?=d)/g, '$1-'));
}
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 12);
$(this).val(inputValue);
}
});
$(this).on('keydown touchend', function (e) {
e = e || window.event;
var key = e.which || e.keyCode; // keyCode detection
var ctrl = e.ctrlKey ? e.ctrlKey : ((key === 17) ? true : false); // ctrl detection
if (key == 86 && ctrl) { // Ctrl + V Pressed !
} else if (key == 67 && ctrl) { // Ctrl + C Pressed !
} else if (key == 88 && ctrl) { // Ctrl + x Pressed !
} else if (key == 65 && ctrl) { // Ctrl + a Pressed !
$(this).trigger("paste");
} else if (e.which != 8 && e.which != 0 && !(e.keyCode >= 96 && e.keyCode <= 105) && !(e.keyCode >= 48 && e.keyCode <= 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
} else if (curchr == 7 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '12');
});
} else if (params.format === '(xxx) xxx-xxxx') {
$(this).on('keydown touchend', function (e) {
e = e || window.event;
var key = e.which || e.keyCode; // keyCode detection
var ctrl = e.ctrlKey ? e.ctrlKey : ((key === 17) ? true : false); // ctrl detection
if (key == 86 && ctrl) { // Ctrl + V Pressed !
} else if (key == 67 && ctrl) { // Ctrl + C Pressed !
} else if (key == 88 && ctrl) { //Ctrl + x Pressed
} else if (key == 65 && ctrl) { //Ctrl + a Pressed !
$(this).trigger("paste");
} else if (e.which != 8 && e.which != 0 && !(e.keyCode >= 96 && e.keyCode <= 105) && !(e.keyCode >= 48 && e.keyCode <= 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3 && e.which != 8 && e.which != 0) {
$(this).val('(' + curval + ')' + " ");
} else if (curchr == 9 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '14');
});
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent && e.originalEvent.clipboardData.getData('Text');
inputValue = inputValue.replace(/D/g, '');
if (!$.isNumeric(inputValue)) {
return false;
} else {
if (inputValue.length > 9) {
inputValue = String(inputValue.replace(/(d{3})(d{3})(d{4})/, "($1) $2-$3"));
} else if (inputValue.length > 6) {
inputValue = String(inputValue.replace(/(d{3})(d{3})(?=d)/g, '($1) $2-'));
} else if (inputValue.length > 3) {
inputValue = String(inputValue.replace(/(d{3})(?=d)/g, '($1) '));
}
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 14);
$(this).val(inputValue);
}
});
}

}
}(jQuery));

使用return false更改else-if以排除tab键。如果是return false,则需要使用:e.keyCode != 9在所有其他情况下更改

else if (e.keyCode != 9 && e.which != 8 && e.which != 0 && !(e.keyCode >= 96 && e.keyCode <= 105) && !(e.keyCode >= 48 && e.keyCode <= 57))

最新更新