当我在输入中添加一个数字时,它不会改变保持在5的值,当我进入控制台并键入uw.value时,我得到“6”而不是6,我输入网络上的输入如何我得到一个号码而不是一个字符串?
<form> <input type="number" id="wakken" type="number" value=5><br> <input max="30" min="0" name="ijsberen" placeholder="Ijsberen" id="ijsberen" type="number" value= "5"><br> <input max="30" min="0" name="wakken" placeholder="Penguins" id="penguins" type="number" value= 0><br> <input type="button" value="submit" onclick="check()"> </form>
var uw = document.getElementById("wakken") var ui = document.getElementById("ijsberen") var up = document.getElementById("penguins")
解决方法
查看HTMLInputElement.
valueAsNumber:
The value of the element,interpreted as one of the following in order:
- a time value
- a number
- 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);
<input type="number" id="test-input" type="number" value=5>
对于非数字或非有限数字,这将返回NaN
.