input 체크박스 더하기 문의드립니다!

input 체크박스 더하기 문의드립니다!

QA

input 체크박스 더하기 문의드립니다!

본문

안녕하세요!

 

 

라디오의 값을 인풋1, 인풋2, 인풋3에 출력되게 하고 

여기서 그 숫자를 더한 결과값을 출력해 주려고 합니다.

 

http://utogether.cafe24.com/bbs/page.php?hid=p201

 


<script>
<!--
function checkfunction()
{
 with(document.all)
 {
  for(i=0; i<a.length; i++)
  {
   if(a[i].checked==true)
   {
    content1.value = a[i].value;
   }
  }

  for(i=0; i<b.length; i++)
  {
   if(b[i].checked==true)
   {
    content2.value = b[i].value;
   }
  }
  for(i=0; i<c.length; i++)
  {
   if(c[i].checked==true)
   {
    content3.value = c[i].value;
   }
  }

 }
}
//-->

<!--
function sum()
{
    var obj = document.sumfrm;
    var sum=0;
    for(i=0;i<obj.content.length;i++)
    {
        if(obj.content[i].value*0 == 0)
        {
            sum += obj.content[i].value*1;
        }
    }
    obj.sum_content.value = sum;
}
//-->
</script>

 

 

제가 개발자가 아니어서 잘 모르는 상태인데, 여기저기서 퍼와가지고 작업한거라 뭐가 뭔지도 모르겠습니다^^;;

 

 


<form name='sumfrm' method="post" action="">

<input type='radio' name='a' value='24200' onclick='javascript:checkfunction()'> 24200
<input type='radio' name='a' value='29700' onclick='javascript:checkfunction()'> 29700
<input type='radio' name='a' value='35200' onclick='javascript:checkfunction()'> 35200

<br><br>
<input type='radio' name='b' value='12100' onclick='javascript:checkfunction()'> 12100
<input type='radio' name='b' value='16500' onclick='javascript:checkfunction()'> 16500
<input type='radio' name='b' value='18700' onclick='javascript:checkfunction()'> 18700
<input type='radio' name='b' value='26300' onclick='javascript:checkfunction()'> 26300
<input type='radio' name='b' value='28800' onclick='javascript:checkfunction()'> 28800
<br><br>
<input type='radio' name='c' value='6600' onclick='javascript:checkfunction()'> 6600
<input type='radio' name='c' value='7700' onclick='javascript:checkfunction()'> 7700
<input type='radio' name='c' value='9900' onclick='javascript:checkfunction()'> 9900
<input type='radio' name='c' value='9900' onclick='javascript:checkfunction()'> 9900
<input type='radio' name='c' value='11000' onclick='javascript:checkfunction()'> 11000
<br><br>
<input type="text" name="content" id='content1' onBlur="sum()" value="0">
<input type="text" name="content" id='content2' onBlur="sum()" value="0">
<input type="text" name="content" id='content3' onBlur="sum()" value="0">
결과 : <input type="text" name="sum_content">

 

 

 

이게 계산은 잘 되는데 꼭 인풋1이나 결과부분을 눌러줘야만 결과값이 나오더라고여;;

 

혹시 해결책이 있거나 힌트가 있다면...

알려주시면 감사하겠습니다!

 

 

 

이 질문에 댓글 쓰기 :

답변 3

function checkfunction() 에 sum(); 을 추가해주면 됩니다.

 

function checkfunction() {

 ~

 sum();

}

 


<script>
function checkfunction()
{
 with(document.all)
 {
  for(i=0; i<a.length; i++)
  {
   if(a[i].checked==true)
   {
    content1.value = a[i].value;
   }
  }
  for(i=0; i<b.length; i++)
  {
   if(b[i].checked==true)
   {
    content2.value = b[i].value;
   }
  }
  for(i=0; i<c.length; i++)
  {
   if(c[i].checked==true)
   {
    content3.value = c[i].value;
   }
  }
 }
 
 sum();

}
//-->
<!--
function sum()
{
    var obj = document.sumfrm;
    var sum=0;
    for(i=0;i<obj.content.length;i++)
    {
        if(obj.content[i].value*0 == 0)
        {
            sum += obj.content[i].value*1;
        }
    }
    obj.sum_content.value = sum;
}
</script>

checkfunction() 함수 마지막에
sum() 함수를 호출해보세요

 

function checkfunction()
{
 with(document.all)
 {
     // .... 생략
    sum();
 }
}


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
1 <input type="radio" name="num1" value="1" id="num1" class="sum"/>
2 <input type="radio" name="num1" value="2" id="num1" class="sum"/> <br />
3 <input type="radio" name="num2" value="3" id="num2" class="sum"/>
4 <input type="radio" name="num2" value="4" id="num2" class="sum"/> <br />
5 <input type="radio" name="num3" value="5" id="num3" class="sum"/>
6 <input type="radio" name="num3" value="6" id="num3" class="sum" /> <br />
7 <input type="radio" name="num4" value="7" id="num4" class="sum"/>
8 <input type="radio" name="num4" value="8" id="num4" class="sum"/> <br />
Total: <input type="text" name="total" id="total"/>
<script>
    $(".sum").click(function() {
      var total = 0;
      $(".radi:checked").each(function() { total += +this.value; });
      $("#total").val(total);
    });
</script>
답변을 작성하시기 전에 로그인 해주세요.
전체 1,268
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT