Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- ๋ณ์
- ์ปจํธ๋กค๋ฌ
- ๋ช ๋ น์ด
- ๋ฐฑํฑ
- ์๋ฐ์คํฌ๋ฆฝํธ
- SQL
- ์ธ๋ผ์ธ๋ทฐ
- JavaScript
- ์ฝํ
- ๋ฆฌ๋ ์ค
- ๋์ปค
- optionํ๊ทธ
- ํจ์
- like
- ๋์
- ํ๋ก๊ทธ๋๋จธ์ค
- select
- ํ ์ด๋ธ
- JS
- Update
- Spring
- JSP
- oracle
- Ajax
- MySQL
- order by
- DATE_FORMAT
- ๋์ ํ ์ด๋ธ
- post๋ฐฉ์
- ๋ฐฐ์ด
Archives
- Today
- Total
bom's happy life
[spring boot, jsp, js] ๋ก๊ทธ์ธ์ ์ ์ ์ฐจ๋์ข ๋ฅ์ ํด๋นํ๋ ์ฃผ์ ํ์ค๋จ๊ฐ๋ก ๊ณ์ฐํ๊ธฐ ๋ณธ๋ฌธ
Deveolpment Study๐๏ธ
[spring boot, jsp, js] ๋ก๊ทธ์ธ์ ์ ์ ์ฐจ๋์ข ๋ฅ์ ํด๋นํ๋ ์ฃผ์ ํ์ค๋จ๊ฐ๋ก ๊ณ์ฐํ๊ธฐ
bompeach 2024. 2. 2. 16:57Controller
//๋ก๊ทธ์ธ์ ์ ์ ์ฐจ์ข
๋ฅ(ex.์ค์คํ์ฐจ)
CarVo userCar = carService.findCarByUserSeq(authUser.getSeq());
if (userCar != null && userCar.getCarKind() != null) {
String userCarKind = userCar.getCarKind();
model.addAttribute("userCarKind", userCarKind);
} else {
model.addAttribute("userCarKind", "null");
}
//์ด๋ฒ ๋ฌ ์ฃผ์ ํ์ค๋จ๊ฐ
List<StdGasPriceVo> list = stdGasPriceService.stdGasPriceByThisMonth(stdGasPriceVo);
model.addAttribute("list" , list);
์ปจํธ๋กค๋ฌ์์ ๋ก๊ทธ์ธ์ ์ ์ ์๋์ฐจ ์ข ๋ฅ๋ฅผ ๊ตฌํ๊ณ ,
์ด๋ฒ๋ฌ ์ฃผ์ ํ์ค๋จ๊ฐ ๋ฆฌ์คํธ๋ฅผ ๊ตฌํ๋ค.
JSP
<span id="userCarKind" style="display:none;">${userCarKind }</span>
---์ค๋ต
<tbody>
<tr>
<td onchange="optionControl()" style="text-align:center;"> <!-- ๊ตฌ๋ถ -->
<select id="carKind" name="flag">
<option value="" >:: ์ ํ ::</option>
<option value="๋ณต๋ฆฌํ์๋น">๋ณต๋ฆฌํ์๋น</option>
<option value="์ฐจ๋์ ์ง๋น">์ฐจ๋์ ์ง๋น</option>
<option value="์ฌ๋น๊ตํต๋น">์ฌ๋น๊ตํต๋น</option>
<option value="๊ธฐํ">๊ธฐํ</option>
</select>
</td>
<td> <!-- ์ ์ฒญ๊ธ์ก -->
<input id="Application_amount" name="amount" type="text" value="" placeholder="0">
</td>
<td> <!-- ์ฃผํ๊ฑฐ๋ฆฌ -->
<input id="distance_driven" name="mileage" type="text" value="" placeholder="0">
</td>
</tr>
</tbody>
---์ค๋ต
<!-- ํ์ฌ ์์ ํด๋นํ๋ ์ฐจ๋์ข
๋ฅ๋ณ ํ์ค๋จ๊ฐ(๊ณ์ฐ์ฉ ๋ฐ์ดํฐ) -->
<div id="carTypePrices" style="display:none">
<c:forEach var="list" items="${list}" varStatus="status">
<div class="price light">${list.light}</div>
<div class="price compact">${list.compact}</div>
<div class="price subMidSize">${list.subMidSize}</div>
<div class="price midSize">${list.midSize}</div>
<div class="price semiLarge">${list.semiLarge}</div>
<div class="price large">${list.large}</div>
<div class="price electric">${list.electric}</div>
</c:forEach>
</div>
JS
//์ฐจ๋ ์ข
๋ฅ๋ณ ๋จ๊ฐ ์ ๋ณด
var carTypePrices = {};
document.querySelectorAll('#carTypePrices .price').forEach(function(element) {
var carType = element.classList[1]; // ๋ ๋ฒ์งธ ํด๋์ค ์ด๋ฆ์ด ์ฐจ๋ ์ข
๋ฅ
var price = Number(element.textContent);
carTypePrices[carType] = price;
});
console.log(carTypePrices);
var carKindMapping = {
'๊ฒฝ์ฐจ': 'light',
'์ํ์ฐจ': 'compact',
'์ค์คํ์ฐจ': 'subMidSize',
'์คํ์ฐจ': 'midSize',
'์ค๋ํ์ฐจ': 'semiLarge',
'๋ํ์ฐจ': 'large',
'์ ๊ธฐ์ฐจ': 'electric'
};
//๊ตฌ๋ถ์ต์
์ ์ด
function optionControl() {
var selectedOption = document.getElementById('carKind').value;
var distanceInput = document.getElementById('distance_driven');
var AppamountInput = document.getElementById('Application_amount');
distanceInput.disabled = false;
AppamountInput.disabled = false;
switch(selectedOption) {
case '๋ณต๋ฆฌํ์๋น':
case '์ฌ๋น๊ตํต๋น':
case '๊ธฐํ':
distanceInput.disabled = true;
break;
case '์ฐจ๋์ ์ง๋น':
AppamountInput.disabled = true;
distanceInput.addEventListener('input', calculateApplicationAmount); // ์ฃผํ๊ฑฐ๋ฆฌ ์
๋ ฅ ์ ๊ณ์ฐ
break;
}
}
function calculateApplicationAmount() {
var userCarKindKorean = document.getElementById('userCarKind').textContent.trim();
var userCarKind = carKindMapping[userCarKindKorean]; // ํ๊ธ ์ฐจ๋ ์ข
๋ฅ๋ช
์ ์๋ฌธ ํค๋ก ๋งคํ
var distance = document.getElementById('distance_driven').value; // ์ฃผํ๊ฑฐ๋ฆฌ ์
๋ ฅ ๊ฐ
var pricePerUnit = carTypePrices[userCarKind]; // ์ฌ์ฉ์ ์ฐจ๋์ ๋จ๊ฐ
// ์ฃผํ๊ฑฐ๋ฆฌ ์
๋ ฅ ๊ฐ์ด ๋น์ด์๊ฑฐ๋ ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ ์ ์ฒญ ๊ธ์ก ํ๋๋ฅผ ๋น์์ฃผ๊ณ ํจ์๋ฅผ ์ข
๋ฃ
if (!distance || isNaN(distance)) {
document.getElementById('Application_amount').value = '';
return; // ํจ์ ์ข
๋ฃ
}
if (distance && pricePerUnit) { // ์ฃผํ๊ฑฐ๋ฆฌ๊ฐ ์ ํจํ๊ณ , ๋จ๊ฐ ์ ๋ณด๊ฐ ์์ ๊ฒฝ์ฐ
var applicationAmount = distance * pricePerUnit; // ๊ฑฐ๋ฆฌ * ํด๋น์,์ฐจ์ข
๋ณ ์ฃผ์ ํ์ค๋จ๊ฐ
document.getElementById('Application_amount').value = applicationAmount;
}
}
โ ์ต์ข ๊ฒฐ๊ณผ : ์ฐจ๋์ ์ง๋น๋ฅผ ์ ํํ๋ฉด ์ ์ฒญ๊ธ์ก input์ด disabled์ฒ๋ฆฌ๋๊ณ , ์ฃผํ๊ฑฐ๋ฆฌ๋ฅผ ์ ๋ ฅํ๋ฉด ์ฃผ์ ํ์ค๋จ๊ฐ๋ก ๊ณ์ฐ๋์ด ์ ์ฒญ๊ธ์ก์ ์ฐํ๋๋ก ์์ฑํ์๋ค.
'Deveolpment Study๐๏ธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ฐ์์๋ ๋ค๋ฅธ์ฝ๋]JSP์์์ seq๋ก ๊ฒฝ๋ก์ด๋, js์์์ seq๋ก ๊ฒฝ๋ก์ด๋ ์ฐจ์ด (0) | 2024.01.27 |
---|---|
์ํ์น ์ํ/์ฌ์์/์์/์ค์ง ๋ช ๋ น์ด (0) | 2024.01.23 |
[Spring Boot] ํ์ด์ง ์ฒ๋ฆฌํ๊ธฐ (0) | 2023.10.23 |
[JARํ์ผ์คํํ๊ธฐ] .jarํ์ผ exportํด์ cmd์์ ์คํํ๊ธฐ(๋ช ๋ น์ด) (0) | 2023.08.18 |
๊ธฐ๋ณธ ์ด๋ก ๋ณต์ต (0) | 2022.06.29 |