Input value is a string instead of a number

前端 未结 4 1701
日久生厌
日久生厌 2020-11-29 13:08

When i add a number to the input it doesn\'t change the value that stays at 5 and when i go in the console and type uw.value i get \"6\" back instead of 6 which i typed in

4条回答
  •  死守一世寂寞
    2020-11-29 13:30

    Check out HTMLInputElement.valueAsNumber:

    The value of the element, interpreted as one of the following in order:

    1. a time value
    2. a number
    3. null if conversion is not possible

    var testInput = document.getElementById("test-input");
    
    function handleInput(e){
      var value = this.valueAsNumber;
      console.log("type: %s, value: %o", typeof value, value);  
    }
    
    testInput.addEventListener("input", handleInput);
      
    handleInput.call(testInput);

    This will return NaN for non-numerics or non-finite numbers.

提交回复
热议问题