3

重複の可能性:
HTMLテキスト入力は数値入力のみを許可します

このスクリプトを使用しました

function isNumberKey(evt)
  {
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;                        
    if (charCode > 31 && (charCode < 47 || charCode > 57))
    return false;
    if (e.shiftKey) return false;
    return true;
 }

しかし、このスクリプトはFirefoxだけでは機能しません。javascriptのShiftキーのユーザーを制限するための代替手段はありますか?

4

3 に答える 3

2

eventFirefoxで定義されていないため、コードが壊れています。のためにテストした後にそれを置いてくださいevt

function isNumberKey(evt)
  {
    var e = evt || window.event; //window.event is safer, thanks @ThiefMaster
    var charCode = e.which || e.keyCode;                        
    if (charCode > 31 && (charCode < 47 || charCode > 57))
    return false;
    if (e.shiftKey) return false;
    return true;
 }

フィドル

次回はエラーコンソールを確認してください。:)

于 2012-06-20T06:49:39.243 に答える
0

Try this code..

$(document).keydown(function (e) {
    if (e.shiftKey) {
        e.preventDefault();
    }
});

check the fiddle..

http://jsfiddle.net/kabichill/8FgH4/

于 2012-06-20T06:47:38.983 に答える
-2

U can restrict any key press (a group of them also) by using jQuery

$(document).ready(function()
{
     $('#PHONE_NUMBER').keypress(function(e) {
            var a = [];
            var k = e.which;

            for (i = 48; i < 58; i++)
            a.push(i);
            a.push(8);
            if (!(a.indexOf(k)>=0))
                e.preventDefault();
            });
});

Where #phone number is the id of the input element and the for loop and push function pushes only allowed characters. 48 to 58 is key values for digits 0-9 on key board

于 2012-06-20T06:43:57.253 に答える