我有2個按鈕,這2個按鈕將增加總點擊次數。 (總數單獨計數) 用戶只允許選擇其中的1個,這意味着如果用戶以前點擊過buttonA並再次點擊buttonB。 ButtonA將 - 1和ButtonB將+1。2個按鈕之間的增量和減量
我有以下代碼,並且存在問題。如果沒有按鈕之前單擊將
buttonA (0) - buttonB (0).
如果用戶點擊按鈕a假設是buttonA (1) - buttonB (0)
但下面的代碼顯示
buttonA (1) - buttonB (-1)
我要的是,該按鈕將僅減少其用戶按鈕點擊它之前。 我該如何改進我的代碼?它似乎凌亂。
$this.addClass('active').siblings().removeClass('active');
if ($this.is('span[name="ans_yes"]')) {
yes_no = 1;
currentVal = parseInt($this.find('.badge').html());
$this.find('.badge').html(currentVal + 1);
currentVal2 = parseInt($id.find('.ans-no').html());
$id.find('.ans-no').html(currentVal2 - 1);
} else {
yes_no = 0;
currentVal = parseInt($this.find('.badge').html());
$this.find('.badge').html(currentVal + 1);
currentVal2 = parseInt($id.find('.ans-yes').html());
console.log(currentVal2);
$id.find('.ans-yes').html(currentVal2 - 1);
}
更新 - demo
在jsfiddle.net創建一個演示與相應的HTML – charlietfl 2013-02-16 17:11:04
@charlietfl我添加了演示 – vzhen 2013-02-16 17:37:09
頁面上是否會出現多個buttonA和buttonB,或者每個只有一個?值是隻有0還是1?我們可以使用更多的細節來弄清楚你實際要做什麼。 – 2013-02-16 17:41:07