Javascript jQuery启用选项卡键输入
我使用下面的jQuery在输入字段中格式化电话号码,效果非常好!除了一件事,它禁用了除数字键以外的所有输入键,这对电话号码很有意义 问题是Tab键也被禁用了,我喜欢在填写表格转到下一个字段时使用它。如何启用Tab键输入Javascript jQuery启用选项卡键输入,javascript,jquery,Javascript,Jquery,我使用下面的jQuery在输入字段中格式化电话号码,效果非常好!除了一件事,它禁用了除数字键以外的所有输入键,这对电话号码很有意义 问题是Tab键也被禁用了,我喜欢在填写表格转到下一个字段时使用它。如何启用Tab键输入 (function ($) { $.fn.usPhoneFormat = function (options) { var params = $.extend({ format: 'xxx-xxx-xxxx',
(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));
(函数($){
$.fn.usPhoneFormat=函数(选项){
变量参数=$.extend({
格式:“xxx xxx xxxx”,
国际:错,
},选项);
如果(params.format=='xxx xxx xxxx'){
$(this).bind('paste',函数(e){
e、 预防默认值();
var inputValue=e.originalEvent&&e.originalEvent.clipboardData.getData('Text');
inputValue=inputValue.replace(/\D/g',);
如果(!$.isNumeric(inputValue)){
返回false;
}否则{
如果(inputValue.length>9){
inputValue=String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/,“$1-$2-$3”);
}否则{
inputValue=String(inputValue.replace(/(\d{3})(?=\d)/g,“$1-”);
}
$(this).val(inputValue);
$(this.val(“”);
inputValue=inputValue.substring(0,12);
$(this).val(inputValue);
}
});
$(此).on('keydown-touchend',函数(e){
e=e | | window.event;
var key=e.which | | e.keyCode;//keyCode检测
var ctrl=e.ctrlKey?e.ctrlKey:((key==17)?true:false);//ctrl检测
如果(键==86&&ctrl){//ctrl+V按下!
}否则,如果按下(键==67&&ctrl){//ctrl+C!
}否则,如果按下(键==88&&ctrl){//ctrl+x!
}否则,如果(键==65&&ctrl){//ctrl+a按下!
$(此).trigger(“粘贴”);
}否则如果(e.which!=8&&e.which!=0&&&!(e.keyCode>=96&&e.keyCode=48&&e.keyCode=96&&e.keyCode=48&&e.keyCode 9){
inputValue=String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/,“($1)$2-$3”);
}否则如果(inputValue.length>6){
inputValue=String(inputValue.replace(/(\d{3})(\d{3})(?=\d)/g'($1)$2-');
}否则如果(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键。这需要在所有else if's中更改,如果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))
else如果(e.keyCode!=9&&e.which!=8&&e.which!=0&&!(e.keyCode>=96&&e.keyCode=48&&e.keyCode)此站点有助于获取JavaScript键码值: