输入值是字符串而不是数字 [英] Input value is a string instead of a number

查看:132
本文介绍了输入值是字符串而不是数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在输入中添加数字时,它不会更改保持为5的值,当我进入控制台并键入uw.value时,我会返回"6",而不是我在输入中键入的6在网络上,我如何取回数字而不是字符串?

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 the input on the web how do i get a number back instead of a string?

<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:

  1. 时间值
  2. 一个数字
  3. 如果无法进行转换,则为空
  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);

<input type="number" id="test-input" type="number" value=5>

这将返回 NaN 用于非数字或非有限数字.

这篇关于输入值是字符串而不是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆