jQuery 将字符串转换为数字并加一

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

Convert string to number and add one

jqueryhtmljquery-ui

提问by Niklas

I want to turn the value I get from the id into a number and add one to it then pass the new value into the dosomething()function to use. When I tried this and the value is one I get back 11 not 2.

我想将从 id 获得的值转换为一个数字,并在其中添加一个,然后将新值传递dosomething()给要使用的函数。当我尝试这个并且值为 1 时,我返回 11 而不是 2。

$('.load_more').live("click",function() { // When user clicks
    var newcurrentpageTemp = $(this).attr("id") + 1;// Get id from the hyperlink
    alert(parseInt(newcurrentpageTemp));
    dosomething();
});

回答by Justin Niessner

Assuming you are correct and your id is a proper number (without any other text), you should parse the id and then add one to it:

假设您是正确的并且您的 id 是一个正确的数字(没有任何其他文本),您应该解析该 id,然后向其添加一个:

var currentPage = parseInt($(this).attr('id'), 10);
++currentPage;

doSomething(currentPage);

回答by Doozer Blake

Have you tried flip-flopping it a bit?

你试过稍微翻转一下吗?

var newcurrentpageTemp = parseInt($(this).attr("id"));
newcurrentpageTemp++;
alert(newcurrentpageTemp));

回答by cyco130

I believe you should add 1 afterpassing it to parseInt

我相信你应该将它传递给 parseInt加 1

$('.load_more').live("click",function() { //When user clicks
          var newcurrentpageTemp = parseInt($(this).attr("id")) + 1;   
          alert(newcurrentpageTemp);
          dosomething();
      });

http://jsfiddle.net/GfqMM/

http://jsfiddle.net/GfqMM/

回答by Sedat Ba?ar

$('.load_more').live("click",function() { //When user clicks
          var newcurrentpageTemp = parseInt($(this).attr("id")) + 1
          dosomething(newcurrentpageTemp );
      });

http://jsfiddle.net/GfqMM/

http://jsfiddle.net/GfqMM/

回答by Jean-Charles

You have to parse the id before adding 1

您必须在添加 1 之前解析 id

 $('.load_more').live("click",function() { //When user clicks
              var newcurrentpageTemp = parseInt($(this).attr("id"));
              newcurrentpageTemp ++;
              dosomething(newcurrentpageTemp );
 });

回答by Jayendra

Parse the Id as it would be string and then add.

将 Id 解析为字符串,然后添加。

e.g.

例如

$('.load_more').live("click",function() { //When user clicks
    var newcurrentpageTemp = parseInt($(this).attr("id")) + 1;//Get the id from the hyperlink
    alert(newcurrentpageTemp);
    dosomething();
});

回答by Chris Snowden

I've got this working in a similar situation for moving to next page like this:

我在类似的情况下进行了这项工作,以便像这样移动到下一页:

$("#page_next").click(function () {
    $("#pageNumber").val(parseInt($("#pageNumber").val()) + 1);
    submitForm(this);
    return false;
});

You should be able to add brackets to achieve what you want something like this:

你应该能够添加括号来实现你想要的东西:

var newcurrentpageTemp = (parseInt($(this).attr("id"))) + 1;//Get the id from the hyperlink

回答by DevlshOne

The simplest solution here is to change

这里最简单的解决方案是改变

  var newcurrentpageTemp = $(this).attr("id") + 1;//Get the id from the hyperlink

to:

到:

  var newcurrentpageTemp = (($(this).attr("id")) * 1) + 1;//Get the id from the hyperlink

回答by njr101

The parseInt solution is the best way to go as it is clear what is happening.

parseInt 解决方案是最好的方法,因为很清楚发生了什么。

For completeness it is worth mentioning that this can also be done with the + operator

为了完整起见,值得一提的是,这也可以使用 + 运算符来完成

$('.load_more').live("click",function() { //When user clicks
  var newcurrentpageTemp = +$(this).attr("id") + 1; //Get the id from the hyperlink
  alert(newcurrentpageTemp);
  dosomething();
});