[英]Adding +1 to jQuery value of a readonly text input field
I am using a function where I have a readonly
text input
, and when I execute the function I want the number value + 1. So let's say I have 60, when I execute the function, the number returned should be 61.我正在使用一个函数,其中我有一个
readonly
文本input
,当我执行该函数时,我想要数字值 + 1。假设我有 60,当我执行该函数时,返回的数字应该是 61。
But instead it's coming out 601, which is just adding the number 1 to the string.但是它出来的是 601,它只是将数字 1 添加到字符串中。 Any clue as to what is going on?
关于发生了什么的任何线索? Subtraction, multiplication and division all work fine.
减法、乘法和除法都可以正常工作。 Here is a snippet
这是一个片段
var num= $("#originalnum").val() + 1;
$("#originalnum").val(num);
And yes i've tried a few different variations, am I missing something?是的,我尝试了一些不同的变体,我错过了什么吗?
A simple unary +
is sufficient to turn a string into a number in this case:在这种情况下,简单的一元
+
足以将字符串转换为数字:
var num = +$("#originalnum").val() + 1;
$("#originalnum").val(num);
The problem is that .val()
returns the value of the element as a string, and when you use the +
operator on a string it does string concatenation.问题是
.val()
将元素的值作为字符串返回,当您在字符串上使用+
运算符时,它会进行字符串连接。 You need to convert the value to a number first:您需要先将值转换为数字:
var num = +$("#originalnum").val() + 1; // unary plus operator
// OR
var num = Number($("#originalnum").val()) + 1; // Number()
// OR
var num= parseFloat($("#originalnum").val()) + 1; // parseFloat()
// OR
var num= parseInt($("#originalnum").val(),10) + 1; // parseInt()
Note that if you use parseInt()
you must include the radix (10) as the second parameter or it will (depending on the browser) treat strings with a leading zero as octal and strings with a leading "0x"
as hexadecimal.请注意,如果您使用
parseInt()
,则必须包含基数 (10) 作为第二个参数,否则它将(取决于浏览器)将带前导零的字符串视为八进制,将带前导"0x"
字符串视为十六进制。 Note also that parseInt()
ignores any non-numeric characters at the end of the string, including a full-stop that the user might have intended as a decimal point, so parseInt("123.45aasdf",10)
returns 123
.另请注意,
parseInt()
忽略字符串末尾的任何非数字字符,包括用户可能希望作为小数点的句号,因此parseInt("123.45aasdf",10)
返回123
。 Similarly parseFloat()
ignores non-numeric characters at the end of the string.同样
parseFloat()
忽略字符串末尾的非数字字符。
Also if it's a user-entered value you should double-check that it actually is a number and perhaps provide an error message if it isn't.此外,如果它是用户输入的值,您应该仔细检查它是否确实是一个数字,如果不是,可能会提供一条错误消息。
When you use the *
, /
or -
operators JS tries to convert the string to a number automatically, so that's why those operators "work" (assuming the string can be converted).当您使用
*
、 /
或-
运算符时,JS 会尝试自动将字符串转换为数字,这就是这些运算符“有效”的原因(假设字符串可以转换)。
You should use the parseInt
function and make sure the value is number(use isNaN function):您应该使用
parseInt
函数并确保值是数字(使用 isNaN 函数):
var val = $("#originalnum").val();
var num = 0;
if ( !isNaN(val) )
num= parseInt(val) + 1;
Use parseInt()
:使用
parseInt()
:
var num= parseInt($("#originalnum").val(),10) + 1;
So your number is treated as an integer instead of a string (as .val()
treats the result as string by default)因此,您的数字被视为整数而不是字符串(因为
.val()
默认将结果视为字符串)
If you don't like the above code spelling, you can try it this way too.如果你不喜欢上面的代码拼写,你也可以这样试试。
$("#originalnum").val(function() {
$(this).val(parseInt($(this).val()) + 1)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.