ํด๋ฆญ ์กฐ๊ฑด.
- ๊ฐ๊ฐ ํด๋น ์ซ์๋ฅผ ํด๋ฆญํ๋ฉด ์ด ํฉ๊ณ๊ฐ ๋์์ผ ํ๋ค.
์ ๋ ฅ ์กฐ๊ฑด.
- ์ํ๋ ์ซ์๋ฅผ ์ ๋ ฅ ์ ์ด ํฉ๊ณ๊ฐ ๋์์ผ ํ๋ค.
- ์ซ์๋ง ์ ๋ ฅ์ด ๊ฐ๋ฅํ๋ค.
0
์ด๋๊ณต๋ฐฑ
์ผ ๊ฒฝ์ฐ(๋์ด์ฐ๊ธฐ ์ ์ธ) ์๋ ํฉ๊ณ๊ฐ ๋์จ๋ค.
<h2>ํด๋ฆญ ์ ๋ง์
๊ตฌํ๊ธฐ</h2>
<ul>
<li class="num">10000</li>
<li class="num">30000</li>
<li class="num">50000</li>
<li class="num">100000</li>
<li class="num">1000000</li>
<li class="total">ํฉ๊ณ</li>
<li class="reset">์ง์ฐ๊ธฐ</li>
</ul>
<br/><br/>
<h2>์
๋ ฅ ์ ๋ง์
๊ตฌํ๊ธฐ</h2>
<input type="text" class="input_num" id="input_01">
<input type="text" class="input_num" id="input_02">
<input type="text" class="input_num" id="input_03">
<input type="text" class="input_total" id="input_total" placeholder="ํฉ๊ณ" readonly>
* { margin: 0; padding: 0; box-sizing: borde-box; }
li { list-style: none; }
ul li { display: inline-block; padding: 10px; margin: 0 5px; min-width: 50px; text-align: center; background: paleturquoise; cursor: pointer; }
.num:hover { background: powderblue; }
.total { background: palevioletred; }
.reset { background: palegreen; }
input { width: 70px; border: 2px solid #999; padding: 10px; }
// jQuery
$(function() {
// click ๋ฒํผ.
var sum = 0;
$('.num').on('click', function() {
$(this).each(function() {
sum += parseInt($(this).text());
})
$('.total').text(sum);
});
// ์ด๊ธฐํ.
$('.reset').on('click', function() {
sum = 0;
$('.total').text(sum);
});
// input ์
๋ ฅ.
var sum2 = parseInt(0);
$('.input_num').on('keyup', function() {
sum2 = parseInt(0);
this.value = this.value.replace(/[^0-9]/g,'');
$('.input_num').each(function() {
if(!isNaN(this.value) && this.value !== '') {
sum2 += parseInt(this.value);
} else {
return 0;
}
$('.input_total').val(sum2);
})
})
});