2011-06-16 61 views
1

我有一系列的輸入按鈕,每個按鈕都有自己的值。jquery - 多個按鈕在點擊時增加相應的值

<ul class="sizeDriller"> 
    <li><input type="button" value="S" class="sizeInput" /></li> 
    <li><input type="button" value="M" class="sizeInput" /></li> 
    <li><input type="button" value="L" class="sizeInput" /></li> 
    <li><input type="button" value="XL" class="sizeInput" /></li> 
</ul> 

如果我點擊一個按鈕,我想每次點擊附加的「1」的數量來相應的按鈕,所以第一個按鈕值應該走「S/1」,則「S/2「,然後是」S/3「等等。

我的劇本是不是真正的工作:

var incr = 0; 
$('.sizeInput').bind('click', function() 
    { 
    var initial = $(this).val(); 
    var init = parseInt(incr); 
    var counter = init+1; 
    $(this).attr('value',initial+'/'+counter); 
    }); 
} 

我想我需要一些類型的循環,但我沒有得到任何地方......

感謝您的幫助!

============================================== ========== 復位按鈕:

<a href="#" class="resetSize">Reset/a> 

復位功能:

$('.resetSize').bind('click', function() 
    { 
    $('.sizeInput').each(function(e) 
    { 
    var current = $(this).val().split("/")[0]; 
    $(this).attr('value',current);    
    }); 
}); 
+0

當使用parseInt函數,使用'10'作爲基數。所以'parseInt(incr)'應該是'parseInt(incr,10)'。另外,要使用jQuery設置value屬性,只需使用'$(this).val(yourValueHere)'。 – artlung 2011-06-16 19:25:32

回答

2

你可以使用data()存儲計數器:

$('.sizeInput').bind('click', function(){ 
    var initial = $(this).val(); 
     if(typeof $(this).data('counter') == "undefined"){ 
      $(this).data('counter',1); 
     }else{ 
      $(this).data('counter',$(this).data('counter')+1); 
      initial = initial.substr(0,initial.indexOf('/')); 
     } 

    $(this).val(initial+'/'+$(this).data('counter')); 
}); 

例如:http://jsfiddle.net/niklasvh/etjYL/

+0

也檢查這一點。非常感謝。 – frequent 2011-06-16 19:24:15

+0

完美。正是我需要的。謝謝! – frequent 2011-06-16 19:51:53

+0

好的,我試圖開發一個全局重置按鈕。作品種類,但重置並嘗試第二次增加後,原始值將消失。我把我的重置功能放在原文中。謝謝你的幫助 – frequent 2011-06-16 22:04:52

0

嘗試var counter = init=+1代替。

1
<ul class="sizeDriller"> 
    <li><input type="button" value="S" /><input type="hidden" name="sizeS" value="0" /></li> 
    <li><input type="button" value="M" /><input type="hidden" name="sizeM" value="0" /></li> 
    <li><input type="button" value="L" /><input type="hidden" name="sizeL" value="0" /></li> 
    <li><input type="button" value="XL" /><input type="hidden" name="sizeXL" value="0" /></li> 
</ul> 

$('.sizeDriller input[type="button"]').bind('click', function(e) { 
    var type = $(this).val().split("/")[0]; 
    var counter = $("input[name='size"+type+"']"); 

    var newcount = parseInt(counter.val())+1; 
    if (newcount > 5) { 
     newcount = 0; 
    } 

    counter.val(newcount); 
    $(this).val(type + (newcount > 0 ? ("/" + newcount) : "")); 
}); 

看到它的工作@http://jsfiddle.net/roberkules/xVeL8/

+0

啊。謝謝!讓我們看看這在我的腳本中看起來如何。 – frequent 2011-06-16 19:23:40

+0

我喜歡你的解決方案,但是來自Niklas的解決方案在我的設置中更易於實施和使用。所以+1,但支票去尼克拉斯。再次感謝! – frequent 2011-06-16 19:51:01

+0

我爲您實際想要發送值的情況添加了隱藏的輸入字段。 – roberkules 2011-06-16 19:53:03

0

怎麼樣像這樣

<input type="button" value="S" initialVal="S" class="sizeInput" onclick="changeValue()"/> 
.... 

function changeValue(){ 

if ($(this).data('counter')){ 
    counter++; 
}else { 
    $(this).data('counter', 1) //set initial to value1 
} 
$(this).val($(this).attr("initialVal")+'/'+$(this).data('counter')) 

}

0
$('.sizeInput').bind('click', function(){ 
    var value = $(this).val(), 
     size = value.split('/')[0], 
       // get part after the slash, if it exists, parse as integer 
       // if there is no slash and part after it, then 0 is value 
       // and then add 1 to the count 
     num = (parseInt(value.split('/')[1], 10) || 0) + 1; 
    $(this).val(size + '/' + num); 
}); 
0

給這一個鏡頭:

<ul class="sizeDriller"> 
    <li><input type="button" value="S" class="sizeInput" /></li> 
    <li><input type="button" value="M" class="sizeInput" /></li> 
    <li><input type="button" value="L" class="sizeInput" /></li> 
    <li><input type="button" value="XL" class="sizeInput" /></li> 
</ul> 

<script> 
$(document).ready(function() { 
var init = 0; 
var counter = 0; 
$('.sizeInput').bind('click', function() 
    { 
    var initial = $(this).val().substring(0,1); 
    var num = $(this).val().substring(2); 
    if (!num) { 
    init = 1 
     counter = 1; 
    } else { 
    init = parseInt(num); 
    counter = init+1; 
    } 
    $(this).attr('value',initial+'/'+counter); 
    }); 
}); 
</script>