[英]jquery update only number
i have <span class="gramatz">(8)</span>
and i would like it to add +1 or take -1 from 8, leaving () untouched, when I click my love
button. 我有<span class="gramatz">(8)</span>
,当我单击“我的love
按钮时,我希望它从8加+1或从-1 <span class="gramatz">(8)</span>
-1。
$(function() {
$(".love").click(function() {
var id = $(this).attr("id");
var dataString = 'id=' + id;
var parent = $(this);
$(this).fadeOut(300);
$.ajax({
type: "POST",
url: "bookmark.php",
data: dataString,
cache: false,
success: function(html) {
parent.html(html);
parent.fadeIn(300);
}
});
return false;
});
});
UPDATE: I forgot I need to decrease the value aswell, check the picture for details: http://img593.imageshack.us/img593/6128/ss20110921031605.png 更新:我忘记了我也需要降低此值,请查看图片以获取详细信息: http : //img593.imageshack.us/img593/6128/ss20110921031605.png
so if user clicks on +, I need to increase the value, if user clicks on - I need to decrease the number. 因此,如果用户单击+,则需要增加该值;如果用户单击-,则需要减少该数。
so if the icon has + it has this <div class="over_img"> </div>
and if - it has this <div class="on_img"> </div>
PS plus an minus icons are generated from div background 因此,如果图标具有+,则具有此<div class="over_img"> </div>
;如果--具有图标,则具有<div class="on_img"> </div>
PS会生成一个减号图标从div背景
$('.gramatz').text(function(i, val){
return '(' + ( +val.replace('(', '').replace(')', '') + 1) + ')';
});
To break that down: 分解:
$('.gramatz').text(function(i, val) {
var num = val.replace('(', '').replace(')', ''); // "8"
num = +num; // Convert from string to number
num = num + 1; // add 1, will now be 9
return '(' + num + ')'; // "(9)"
});
If you want to use a regex, you can use this: 如果要使用正则表达式,可以使用以下命令:
$('.gramatz').text(function(i, val){
return '(' + ( +val.replace(/[^\d]/g, '') + 1) + ')';
});
And here's the fiddle: http://jsfiddle.net/gaA2J/ 这是小提琴: http : //jsfiddle.net/gaA2J/
For your particular case, use this: 对于您的特定情况,请使用以下命令:
$('.over_img, .on_img').click(function(){
var amount = $(this).hasClass('over_img') ? 1 : -1 ;
$('.gramatz').text(function(i, val){
return '(' + ( +val.replace(/[^\d]/g, '') + amount) + ')';
});
});
And here's the fiddle: http://jsfiddle.net/gaA2J/1/ 这是小提琴: http : //jsfiddle.net/gaA2J/1/
Only the incrementing code is shown: 仅显示递增代码:
$(function() {
$(".love").click(function() {
$('.gramatz').text($('.gramatz').text().replace(/(\d+)/, function(str, val) {
return +val + 1;
}))
});
});
$(function() { $(".love").click(function() { var $class = $(this).attr('class'), inc = $class.indexOf('minus') === -1 ? 1: -1; $('.gramatz').text($('.gramatz').text().replace(/(-?\\d+)/, function(str, val) { return +val + inc ; })) }); });
需要使用.live()
完成技巧
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.