๐ŸฃcodeLab

ํด๋ฆญ ๋˜๋Š” ์ˆซ์ž ์ž…๋ ฅ ์‹œ ๋ง์…ˆ ๊ตฌํ•˜๊ธฐ

jmjm 2021. 8. 3. 17:42

ํด๋ฆญ ์กฐ๊ฑด.

  • ๊ฐ๊ฐ ํ•ด๋‹น ์ˆซ์ž๋ฅผ ํด๋ฆญํ•˜๋ฉด ์ด ํ•ฉ๊ณ„๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค.

์ž…๋ ฅ ์กฐ๊ฑด.

  • ์›ํ•˜๋Š” ์ˆซ์ž๋ฅผ ์ž…๋ ฅ ์‹œ ์ด ํ•ฉ๊ณ„๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค.
  • ์ˆซ์ž๋งŒ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • 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);
        })
    })
});