开发者

using jquery to increment values without refreshing on click

开发者 https://www.devze.com 2023-03-29 19:08 出处:网络
I\'m having problems with incremental values without refreshing the page using jquery. I have html code that does this.

I'm having problems with incremental values without refreshing the page using jquery.

I have html code that does this.

<li c开发者_Python百科lass=''>
<img src="../icons/fav_c.png" width='12' style='vertical-align:middle;'/>
&nbsp;&nbsp;
<div style='float:right;padding-top:2px;width:5px'>
<span class='selector'>
<?php echo $value; ?>
</span>
</div>
</li>

I have a code that does in jquery.

val value = $('span.selector').text();
$('span.selector').text(value++);

This is not affecting my value that is being echo'd out? Can anyone see what the problem is? Does .text() not work?

Thanks!


Ok:

var value = $('span.selector').text();
value = parseInt(value, 10); // Use a radix!!!
$('span.selector').text(++value);
  1. ) parseInt with radix with out it '09' for example would be 0. Converts string to an integer

  2. ) ++value - increment value before it's applied to .text(...). value++ would call text() first then increment.


Try:

var value = $('span.selector').text();
value++;
$('span.selector').text(value);

Fiddle


.text expects a string

Description: Get the combined text contents of each element in the set of matched elements, including their descendants.

http://api.jquery.com/text/

try

var value = $('span.selector').text();
value = parseInt(value);
value++;
$('span.selector').text(value++);

here is the fiddle http://jsfiddle.net/XGGPw/


You can also try this

$("span.selector").text(function(i, v) {return parseInt(v, 10) + 1;});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号