using jquery to increment values without refreshing on click
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;'/>
<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);
) parseInt with radix with out it '09' for example would be 0. Converts string to an integer
) ++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;});
精彩评论