HTML5 input type number not working in firefox

前端 未结 7 1414
闹比i
闹比i 2020-12-11 17:48

I am using HTML5 input type=number. Its working perfectly in Chrome browser, but its not working in Firefox and IE9.

I want to increment the quantity by

7条回答
  •  爱一瞬间的悲伤
    2020-12-11 18:12

    I am using firefox, I had the same issue developing my input type number typing characters and spaces etc... anyway I am using angular 2 in this example, it's almost similar to JavaScript, so you can use this code in every case : here is the html :

    
    

    here is the function FilterInput :

    FilterInput(event: any) {
            let numberEntered = false;
            if ((event.which >= 48 && event.which <= 57) || (event.which >= 37 && event.which <= 40)) { //input number entered or one of the 4 directtion up, down, left and right
                //console.log('input number entered :' + event.which + ' ' + event.keyCode + ' ' + event.charCode);
                numberEntered = true;
            }
            else {
                //input command entered of delete, backspace or one of the 4 directtion up, down, left and right
                if ((event.keyCode >= 37 && event.keyCode <= 40) || event.keyCode == 46 || event.which == 8) {
                    //console.log('input command entered :' + event.which + ' ' + event.keyCode + ' ' + event.charCode);
                }
                else {
                    //console.log('input not number entered :' + event.which + ' ' + event.keyCode + ' ' + event.charCode);
                    event.preventDefault();
                }
            }
            // input is not impty
            if (this.validForm) {
                // a number was typed
                if (numberEntered) {
                    let newNumber = parseInt(this.numberVoucher + '' + String.fromCharCode(event.which));
                    console.log('new number : ' + newNumber);
                    // checking the condition of max value
                    if ((newNumber <= 30 && newNumber >= 1) || Number.isNaN(newNumber)) {
                        console.log('valid number : ' + newNumber);
                    }
                    else {
                        console.log('max value will not be valid');
                        event.preventDefault();
                    }
                }
                // command of delete or backspace was types
                if (event.keyCode == 46 || event.which == 8) {
                    if (this.numberVoucher >= 1 && this.numberVoucher <= 9) {
                        console.log('min value will not be valid');
                        this.numberVoucher = 1;
                        //event.preventDefault();
                        this.validForm = true;
                    }
                }
            }
            // input is empty
            else {
                console.log('this.validForm = true');
                this.validForm = false;
            }
        };
    

    in this function I had to just let the keypress of numbers, direction, deletes enter.

提交回复
热议问题