jeudi 3 mars 2016

How to sum value of radio button and checkbox in jquery

I have a code already working which calculates the sum of the radio buttons. I changed my mind in one of the radio button group and decided to make it a checkbox. When I tick items on the checkbox, the sum returns NaN. What would I add/change in my jquery in order for it to recognize the checkbox value?

Here's my code:

JQuery

 < script type = "text/javascript" >
   function calcscore() {
     $(".calc:checked").each(function() {
       score += parseInt($(this).val(), 10);
     });
     $('#price').text(score.toFixed(2));
     $("input[name=sum]").val(score)
   }
 $().ready(function() {
   $(".calc").change(function() {
     calcscore()
   });
 }); 
< /script>
<script src="http://ift.tt/1eH01Db"></script>
<li>
  <label>
    <input class="calc" type="radio" name="rad1" id="rad1" />
    
  </label>
  <input type="hidden" name="rad1" value="100">
</li>


<li>
  <label>
    <input class="calc" type="checkbox" name="check1" id="check1" value="200" />
    
  </label>
  <input type="hidden" name="check1" value="200">
</li>

<p>Total: PHP <span id="price">0</span>
</p>

Appreciate all the help.




Aucun commentaire:

Enregistrer un commentaire