게으른 완벽주의자의 개발자 도전기
[shop]상품 상세페이지 총가격 구현하기 본문
수량 변경했을 때 총가격도 변경 되도록 구현하고자 한다.
1. html
<div>
가격 : <span th:text="${#numbers.formatCurrency(item.itemPrice)}"
id="itemPriceSpan" th:data-item-price ="${item.itemPrice}"></span> won
</div>
<div>
수량 : <input type="number" min="1" value="1" name="cartAmount" id="cartAmount">
<!-- 아이템코드 --><input type="hidden" name="itemCode" th:value="${item.itemCode}"
id="itemCode">
</div>
<div>
총 가격 : <span th:text="${#numbers.formatCurrency(item.itemPrice)}"
id="totalPriceSpan"></span> won
</div>
2. js
const cartAmountInput = document.querySelector('#cartAmount');
cartAmountInput.addEventListener('change', function(){
const itemPrice = document.querySelector('#itemPriceSpan').dataset.itemPrice;
const cartAmount = document.querySelector('#cartAmount').value;
const result = itemPrice * cartAmount;
document.querySelector('#totalPriceSpan').innerText = '₩' + result.toLocaleString();
})
'Spring Boot' 카테고리의 다른 글
[shop] 구매내역조회 화면 구현하기 (1) | 2022.10.23 |
---|---|
[shop] 장바구니에서 구매하기 구현 (0) | 2022.10.23 |
[shop]장바구니 구현하기3 (선택 삭제, 선택 구매) (0) | 2022.10.23 |
[shop] 장바구니 구현하기2(장바구니 리스트, 수량 변경) (0) | 2022.10.23 |
[shop] 장바구니 구현하기1(체크박스, 총가격) (0) | 2022.10.23 |