I'm just trying to get a number from a hidden form input and plus 1 to that number 开发者_运维百科and then put that value to the incremented number.
Here is my code
jQuery('#add_slider_image').live('click', function() {
var slideId = jQuery('#count-level').attr('value');
jQuery('#count-level').attr('value',slideId+1);
});
What am I doing wrong? as it changes the value to 11 or even 111 depending on the amount of clicks. It needs to add the value with 1. like 1+1=2 not 11.
Thanks
its converting it to a string. make sure to do
var slideId = parseInt(jQuery('#count-level').attr('value'));
It sounds like the variable needs to be converted from string to integer. Not positive, but this may work:
change:
jQuery('#count-level').attr('value',slideId+1);
to:
jQuery('#count-level').attr('value',parseInt(slideId)+1);
More on jquery Integers and parsing numbers: http://docs.jquery.com/Types#Integer
精彩评论