用户在文本字段中输入的数字的数值 [英] numerical value of number input by user in a text field
本文介绍了用户在文本字段中输入的数字的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将用户输入的两个数字相加。为此,我创建了两个输入字段,使用.val()从两个单独的变量中检索值,然后添加它们。问题是字符串被添加,而不是数字。例如。 2 + 3变成23而不是5.请建议做什么,除了在输入框中使用type = number。
解决方案
您可以使用parseInt(...)
:
var num = parseInt(2,10)+ parseInt(3,10);
// num == 5
I need to add up two numbers input by the user. To do that, I create two input fields, retrieve values from them , using .val(), in two separate variables and then add them. The problem is that the strings are added and not the numbers. For eg. 2 + 3 becomes 23 and not 5. please suggest what to do, except using type = number in the input boxes.
解决方案
You can use parseInt(...)
Example:
var num = parseInt("2", 10) + parseInt("3", 10);
// num == 5
这篇关于用户在文本字段中输入的数字的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文