채택완료

javascirpt 함수 안에 다른 함수 호출 질문이 있습니다.

2017-08-31 (목) 12:56:04 2,313

function a(){

 total = ga.value+ga2.value;

$(".result").text(total);

}

function b(chk){

   var post_f = 0;

   if(chk.checked == true){

      post_f = 3000;

   }else{

      post_f = 0;

   }

}

이런 형식인데 하고싶은것은 a함수의 total 값에 b 함수의 post_f 값을 더하고싶은데 어떻게 해야할지 여쭈어봅니다..

* b함수는 체크박스가 체크되면 3000+ , 체크를 해제하면 0 원을 추가하는 형식입니다.

답변 1개

채택된 답변
+20 포인트
Copy
<!DOCTYPE html><html><head>    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>    <script type="text/javascript">        function a(){            var ga = $("#ga").get(0);            var ga2 = $("#ga2").get(0);            total = Number(ga.value)+Number(ga2.value);            total += b($("#chk1").get(0));            $(".result").text(total);        }        function b(chk){           var post_f = 0;           if(chk.checked == true){              post_f = 3000;          }else{              post_f = 0;          }          return post_f;      }  </script></head><body>    <input id="ga" type="text" value="10" />    <input id="ga2" type="text" value="20" />    <input id="chk1" type="checkbox" />    <input type="button" value="click" onclick="a()" />    <div class="result"></div></body></html>

답변을 작성하려면 로그인이 필요합니다.