0

i have a javascript code like below,

if (event.keycode != 37 && event.keycode != 39)
{
    var phoneNumber = $('#phoneNumber').val();
    if (phoneNumber.length < 1 && event.keyCode != 48)
        $('#phoneNumber').val(0)
    else if ((phoneNumber.length < 2 && event.keyCode == 48) )
        event.preventDefault();
    else
        $('#phoneNumber').val(phoneNumber)
}

keycode 37 = left arrow, keycode 39 = right arrow but when i pressed these keys on keyboard condition which is between if block being executed, i am using chrome browser, also i used the if statement below,

if (event.keycode != 37 || event.keycode != 39)
{
    var phoneNumber = $('#phoneNumber').val();
    if (phoneNumber.length < 1 && event.keyCode != 48)
        $('#phoneNumber').val(0)
    else if ((phoneNumber.length < 2 && event.keyCode == 48) )
        event.preventDefault();
    else
        $('#phoneNumber').val(phoneNumber)
}

urgent helps greatly appreciated,

Thanks everybody.

4

1 回答 1

0

One issue is capitalization: it's keyCode, not keycode. Your code is using them inconsistently.

于 2013-02-06T12:10:09.270 回答