Javascript 将 +1 添加到只读文本输入字段的 jQuery 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13464007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:09:36  来源:igfitidea点击:

Adding +1 to jQuery value of a readonly text input field

javascriptjquery

提问by Dnaso

I am using a function where I have a readonlytext 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.

我正在使用一个有readonlytextinput的函数,当我执行该函数时,我想要数字值 + 1。所以假设我有 60,当我执行该函数时,返回的数字应该是 61。

But instead it's coming out 601, which is just adding the number 1 to the string. Any clue as to what is going on? Subtraction, multiplication and division all work fine. Here is a snippet

但是它出来的是 601,它只是将数字 1 添加到字符串中。关于发生了什么的任何线索?减法、乘法和除法都可以正常工作。这是一个片段

 var num= $("#originalnum").val() + 1;
 $("#originalnum").val(num);

And yes i've tried a few different variations, am I missing something?

是的,我尝试了一些不同的变体,我错过了什么吗?

回答by Eric

A simple unary +is sufficient to turn a string into a number in this case:

+在这种情况下,简单的一元足以将字符串转换为数字:

var num = +$("#originalnum").val() + 1;
$("#originalnum").val(num);

回答by nnnnnn

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. You need to convert the value to a number first:

问题是.val()将元素的值作为字符串返回,当您+在字符串上使用运算符时,它会进行字符串连接。您需要先将值转换为数字:

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 mustinclude 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. 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. Similarly parseFloat()ignores non-numeric characters at the end of the string.

请注意,如果你使用parseInt(),你必须包括基数(10)作为第二个参数,否则会(视浏览器)治疗用字符串作为八进制和字符串前导零与一家领先的"0x"为十六进制。另请注意,parseInt()忽略字符串末尾的任何非数字字符,包括用户可能希望作为小数点的句号,因此parseInt("123.45aasdf",10)返回123. 同样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 canbe converted).

当您使用*,/-运算符时,JS 会尝试自动将字符串转换为数字,这就是这些运算符“工作”的原因(假设字符串可以转换)。

回答by Kirill Ivlev

You should use the parseIntfunction 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;

回答by DarkAjax

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()默认情况下将结果视为字符串)