projectbase/public/js/admin/service.js
2026-02-10 15:00:16 +09:00

34 lines
1.3 KiB
JavaScript

const serverSelect = document.querySelector("select[name=serverinfo_uid]");
const rackSelect = document.querySelector("select[name=rack]");
const lineSelect = document.querySelector("select[name=line]");
const titleInput = document.querySelector("input[name=title]");
const amountInput = document.querySelector("input[name=amount]");
function getTotalPrice() {
const serverPrice = serverSelect?.options[serverSelect.selectedIndex]?.getAttribute("data-price") || 0;
const rackPrice = rackSelect?.options[rackSelect.selectedIndex]?.value || 0;
const linePrice = lineSelect?.options[lineSelect.selectedIndex]?.value || 0;
return Number(serverPrice) + Number(rackPrice) + Number(linePrice);
}
// 공통 업데이트 함수
function setAmount() {
if (amountInput) {
amountInput.value = getTotalPrice();
}
}
// 이벤트 리스너 등록
if (rackSelect) rackSelect.addEventListener("change", setAmount);
if (lineSelect) lineSelect.addEventListener("change", setAmount);
// ✅ select2는 전용 이벤트 사용
if (serverSelect) {
$(serverSelect).on("select2:select", function () {
setAmount();
if (titleInput) {
titleInput.value = serverSelect?.options[serverSelect.selectedIndex]?.text || "";
}
});
}
//페이지 로드 시 초기 실행
document.addEventListener("DOMContentLoaded", setAmount);