最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to parseInt a jQuery value? - Stack Overflow

programmeradmin4浏览0评论

I'm trying to add +1 to a value but I can't figure out how is the value define. Is it a jQuery object ?

   var value = $(this).attr('data-id'); /* return a number between 0 to xx */
   var val = (value+1);

I tried too : var val = (paseInt(value)+1); with no more effectiveness ..

It tells me that val = 0 when it should be equal to 1 and val = 1 when it should be equal to 2 etc..

A quick help would be greatly appreciate here :)

I'm trying to add +1 to a value but I can't figure out how is the value define. Is it a jQuery object ?

   var value = $(this).attr('data-id'); /* return a number between 0 to xx */
   var val = (value+1);

I tried too : var val = (paseInt(value)+1); with no more effectiveness ..

It tells me that val = 0 when it should be equal to 1 and val = 1 when it should be equal to 2 etc..

A quick help would be greatly appreciate here :)

Share Improve this question asked Jul 22, 2015 at 11:54 BaldrániBaldráni 5,6388 gold badges57 silver badges82 bronze badges 7
  • 1 check dis stackoverflow./questions/9775375/parseint-with-jquery – Light Commented Jul 22, 2015 at 11:55
  • 1 Can you post your html code? And also try with accessing id using $(this).data('id'); – Sachin I Commented Jul 22, 2015 at 11:56
  • what is ing inside the value variable – wiretext Commented Jul 22, 2015 at 11:56
  • @Light This is a good trail; It not the value i'm looking for but something like this should work – Baldráni Commented Jul 22, 2015 at 11:59
  • 3 typo in your parseInt. You have written paseInt which should be parseInt. – Rohit Arora Commented Jul 22, 2015 at 12:03
 |  Show 2 more ments

4 Answers 4

Reset to default 4

Try parsing to int as soon as you fetch data as below:

DEMO HERE

LOCAL DEMO

$('.specldiv').on('click',function(){
   var value = parseInt($(this).attr('data-id')); 
   var vals = (value+1); 
    alert(vals);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="specldiv" data-id="1">Div with id data-id 1</div>
<div class="specldiv" data-id="0">Div with id data-id 0</div>
<div class="specldiv" data-id="2">Div with id data-id 2</div>

Try this:

var value = 0;
value = $(this).attr('data-id');

For data attribute you can just try $(this).data("id") than you can perform following

var value = $(this).data("id");
var val = value + 1;

Use this javascript hack:)

var value = +($(this).attr('data-id')); /* return a number between 0 to xx */
var val = value+1;

When you use "+" without blanks, before string that contains number, your string transforms to number.

发布评论

评论列表(0)

  1. 暂无评论