【JS】jQuery實作數量選擇器

【JS】jQuery實作數量選擇器


數量選擇器

1
2
3
<button type="button" class="reduce">-</button>
<span>0</span>
<button type="button" class="add">+</button>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//數量初始值
let num = 0;

$(".reduce").click(function(){
  //每次點擊數量-1,num = num - 1;
  num-=1;
  $("span").text(num);
});

$(".add").click(function(){
  //每次點擊數量+1,num = num + 1;
  num+=1;
  $("span").text(num);
});

其他相關