嗨在文本框中,当有人输入电话号码时,它会自动将其转换为(xxx)-xxx-xxxx这个格式,我面临的问题是在我输入电话号码后按退格键不是删除(xxx)-xxx之后,我无法在代码中找到问题PLZ帮助我。
$(document).ready(function()
{
$('#telephone').keypress(function(e)
{
var key = e.charCode || e.keyCode || 0;
var keychar = String.fromCharCode(key);
if ( ( ( key == 8 || key == 9 || key == 46 || key == 35 || key == 36 || (key >= 37 && key <= 40) ) && e.charCode==0 ) || (key >= 48 && key <= 57) && ($('#telephone').val().length < 14) )
{
var tlength = $('#telephone').val().length;
var tvalue = $('#telephone').val();
if(tlength == 3)
{
var tvalue = '(' + $('#telephone').val() + ')'
$('#telephone').val(tvalue);
}
var tlength = $('#telephone').val().length;
var tvalue = $('#telephone').val();
if(tlength == 5)
{
var tvalue = $('#telephone').val() + '-';
$('#telephone').val(tvalue);
}
var tlength = $('#telephone').val().length;
var tvalue = $('#telephone').val();
if( tlength == 9 )
{
var tvalue = $('#telephone').val() + '-';
$('#telephone').val(tvalue);
}
return;
}
else
{
e.preventDefault();
}
});
});
答案 0 :(得分:0)
我认为这一行的问题......
if ( ( ( key == 8 || key == 9 || key == 46 || key == 35 || key == 36 || (key >= 37 && key <= 40) ) && e.charCode==0 ) || (key >= 48 && key <= 57) && ($('#telephone').val().length < 14) )
尝试更改该行并将其替换为:
if ( ( ( key == 8 || key == 9 || key == 46 || key == 35 || key == 36 || (key >= 37 && key <= 40) ) && key == 0 ) || (key >= 48 && key <= 57) && ($('#telephone').val().length < 14) )