如何将HTML输入值的数据类型更改为整数? [英] How can I change an HTML input value's data type to integer?
问题描述
我正在使用jQuery检索由输入按钮提交的值.该值应为整数.我想将其递增一并显示.
I'm using jQuery to retrieve a value submitted by an input button. The value is supposed to be an integer. I want to increment it by one and display it.
// Getting immediate Voting Count down button id
var countUp = $(this).closest('li').find('div > input.green').attr('id');
var count = $("#"+countUp).val() + 1;
alert (count);
上面的代码给了我一个串联的字符串.假设值是3.我想获得4作为输出,但是代码产生31.
The above code gives me a concatenated string. Say for instance the value is 3. I want to get 4 as the output, but the code produces 31.
如何将HTML输入值的数据类型更改为整数?
How can I change an HTML input value's data type to integer?
推荐答案
要将strValue
转换为整数,请使用:
To convert strValue
into an integer, either use:
parseInt(strValue, 10);
或一元+
运算符.
+strValue
请注意parseInt
的基数参数,因为前导0将导致parseInt
假定输入为八进制,而输入010
的值将为8而不是10
Note the radix parameter to parseInt
because a leading 0 would cause parseInt
to assume that the input was in octal, and an input of 010
would give the value of 8 instead of 10
这篇关于如何将HTML输入值的数据类型更改为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!