如何使用 Javascript/jQuery 将数字字符串增加 +1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11295066/
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 05:20:46  来源:igfitidea点击:

How to increment a numeric string by +1 with Javascript/jQuery

javascriptjquery

提问by JayDee

I have the following variable:

我有以下变量:

pageID = 7

I'd like to increment this number on a link:

我想在链接上增加这个数字:

$('#arrowRight').attr('href', 'page.html?='+pageID);

So this outputs 7, I'd like to append the link to say 8. But if I add +1:

所以这输出 7,我想附加链接说 8。但是如果我添加 +1:

$('#arrowRight').attr('href', 'page.html?='+pageID+1);

$('#arrowRight').attr('href', 'page.html?='+pageID+1);

I get the following output: 1.html?=71 instead of 8.

我得到以下输出:1.html?=71 而不是 8。

How can I increment this number to be pageID+1?

如何将此数字增加为 pageID+1?

回答by antyrat

Try this:

尝试这个:

parseInt(pageID, 10) + 1

Accordint to your code:

根据您的代码:

$('#arrowRight').attr('href', 'page.html?='+ (parseInt(pageID, 10) + 1));

回答by Oleg V. Volkov

+happens to be valid operator for both strings and numbers that gives different results when both arguments are numeric and when at least one is not. One of possible workarounds is to use operator that only have numeric context but gives same mathematical result, like -. some_var - -1will always be same as adding 1 to some_var's numeric value, no matter if it is string or not.

+恰好是字符串和数字的有效运算符,当两个参数都是数字时,当至少有一个不是时,会给出不同的结果。一种可能的解决方法是使用仅具有数字上下文但给出相同数学结果的运算符,例如-. some_var - -1将始终与将 1 添加到some_var的数值相同,无论它是否是字符串。

$('#arrowRight').attr('href', 'page.html?='+ (pageID - -1));

回答by ninja

It needs to be a integer, not a string. Try this:

它需要是一个整数,而不是一个字符串。尝试这个:

pageID = parseInt(pageID)+1;

Then you can do

然后你可以做

$('#arrowRight').attr('href', 'page.html?='+pageID);

回答by sookool99

All these solutions assume that your number you want to add 1 to is within the machine precision for an integer. So if you have a large enough number within that string when you add 1 to it won't change the number.

所有这些解决方案都假设您要加 1 的数字在整数的机器精度范围内。因此,如果该字符串中有足够大的数字,则在向其添加 1 时不会更改该数字。

For Example:

例如:

parseInt('800000000000000000', 10) + 1 = 800000000000000000

So I wrote a quick solution to the problem

所以我写了一个快速解决问题的方法

function addOne(s) {
    let newNumber = '';
    let continueAdding = true;
    for (let i = s.length - 1; i>= 0; i--) {
        if (continueAdding) {
            let num = parseInt(s[i], 10) + 1;
            if (num < 10) {
                newNumber += num;
                continueAdding = false;
            } else {
                newNumber += '0';
            }
        } else {  
            newNumber +=s[i];
        }
    }
    return newNumber.split("").reverse().join("");
}

Now, using the same example above

现在,使用上面相同的示例

addOne('800000000000000000') + 1 = '800000000000000001'

Note that it must stay as a string or you will lose that 1 at the end.

请注意,它必须保留为字符串,否则最后将丢失 1。

回答by ericchan1336

let pageId = '7'
pageId++
console.log(pageId)

Nowadays, you just need to pageID++.

如今,您只需要 pageID++。

回答by Shikiryu

Simply, $('#arrowRight').attr('href', 'page.html?='+(pageID+1));

简单地, $('#arrowRight').attr('href', 'page.html?='+(pageID+1));

The parentheses makes the calculation done first before string concatenation.

括号使计算在字符串连接之前先完成。

回答by Hymanwanders

Just change your order of operations by wrapping your addition in parentheses; if pageID is already a number, parseInt() isn't necessary:

只需将添加内容括在括号中,即可更改操作顺序;如果 pageID 已经是数字,则不需要 parseInt():

$('#arrowRight').attr('href', 'page.html?='+(pageID+1));

回答by Brett Zamir

As long as your pageID is numeric, this should be sufficient:

只要您的 pageID 是数字,这就足够了:

$('#arrowRight').attr('href', 'page.html?='+(pageID+1));

The problem you were seeing is that JavaScript normally executes in left-to-right order, so the string on the left causes the +to be seen as a concatenator, so it adds the 7 to the string, and then adds 1 to the string including 7.

您看到的问题是 JavaScript 通常按从左到右的顺序执行,因此左侧的字符串导致+被视为连接器,因此它将 7 添加到字符串中,然后将 1 添加到字符串中,包括7.