javascript - Displaying radio button value total -


i working on interactive checklist needs count how many "yes" , "no" answers user has selected , display total either in alert box or on page. yes answer gets value of 1, , no gets value of 0.

the problem is, when clicking total button, displaying value of each answer , total - need display total.

i have poked around solutions, , have modified code based on have found. pretty new javascript, must missing something.

here code:

    <script type="text/javascript"><!--     $(document).ready(function () {     $('input:radio').on('change',function(){     var $first_question = parseint($("input[name=first_answer]:checked").val());     var $second_question = parseint($("input[name=second_answer]:checked").val());     $first_question = $first_question? $first_question : 0;      $second_question = $second_question? $second_question : 0;      var $total_score = $first_question + $second_question;      {    $("#btn").click(function() {    alert($total_score);    });    };     });    });    //--></script> 

and here html:

    <p id="first_question">first question<input name="first_answer" class="styled"    type="radio" value="1" /><!-- positive answer -->yes <input name="first_answer" class="styled" type="radio" value="0" /><!-- negative answer -->no</p>     <p id="second_question">second question <input name="second_answer" class="styled" type="radio" value="1" /><!-- positive answer -->yes <input name="second_answer" class="styled" type="radio" value="0" /><!-- negative answer -->no</p>     <input id="btn" type="button" value="see total" /> <input type="reset" value="reset" /></body> 

is this want?

var $total_score = 0;  $(document).ready(function () {     $('input:radio').on('change',function(){         var $first_question = parseint($("input[name=first_answer]:checked").val());         var $second_question = parseint($("input[name=second_answer]:checked").val());         $first_question = $first_question? $first_question : 0;          $second_question = $second_question? $second_question : 0;         $total_score = $first_question + $second_question;     });     $("#btn").click(function() {        alert($total_score);     });  }); 

if is, error added event click button every time radio button changes value.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -