[JS] 금액 콤마

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<style type="text/css">
*{margin:0;padding:0;}
</style>
</head>

<body>


<div class="set po1">
<input type="text" name="item01" id="item01" readonly="readonly" value="1" />
<a href="#" class="btn_act1">+</a>
<a href="#" class="btn_act2">-</a>
</div>


<div class="set po2">
<input type="text" name="item02" id="item02" readonly="readonly" value="0" />
<a href="#" class="btn_act1">+</a>
<a href="#" class="btn_act2">-</a>
</div>

<input type="button" class="btnCheck" id="btnCheck" value="확인" />

<p class="won" id="result">-</p>



<script type="text/javascript">
$(document).ready(function(){
$('#btnCheck').on('click', function(){

var item01 = $('#item01').val(),
item02 = $('#item02').val(),
result = $('#result');


if(item01 == 0 && item02 == 0) {
alert("갯수를 선택");
} else {
var itemPrice01 = 10000,
itemPrice02 = 20000,
price = (item01*itemPrice01) + (item02*itemPrice02);

result.html(price);

result.text(function(){
$(this).text(
$(this).text().format()
);
});
}

});

$(".set a").on("click",function(e){
e.preventDefault();
var key;
if($(this).siblings("input").val() == ""){
key = 0;
}else{
key = $(this).siblings("input").val();
}

if($(this).hasClass("btn_act1") == true){
key++;
}else{
key--;
}
if(key < 0){
key = 0;
}
$(this).siblings("input").val(key);
});
});

// 숫자 타입에서 쓸 수 있도록 format() 함수 추가
Number.prototype.format = function(){
if(this==0) return 0;

var reg = /(^[+-]?\d+)(\d{3})/;
var n = (this + '');

while (reg.test(n)) n = n.replace(reg, '$1' + ',' + '$2');

return n;
};
// 문자열 타입에서 쓸 수 있도록 format() 함수 추가
String.prototype.format = function(){
var num = parseFloat(this);
if( isNaN(num) ) return "0";

return num.format();
};

</script>








</body>
</html>

댓글

이 블로그의 인기 게시물

[iOS] body 스크롤 막기

[html] 모바일웹 작업 팁 정리중...

Swiper lazyLoading