|
Mailing Lists
|
Home /
Groups /
JQuery
summing radio buttons
This should be simple. I have a few radio buttons and when each is clicked,Michael Dinowitz 06/14/11 11:09 A You forgot to wrap it in document.ready. The code fails becauseRaymond Camden 06/14/11 11:30 A You also want to parseInt your values - I'm getting string additions,Raymond Camden 06/14/11 11:30 A This worked for me:Raymond Camden 06/14/11 11:31 A >> total += parseInt($(this).val());Gerald Guido 06/14/11 11:39 A Um - no idea. :)Raymond Camden 06/14/11 11:47 A That was one of my test variables. ParseInt, ParseFloat, no parse function,Michael Dinowitz 06/14/11 12:05 P Note:Phillip Senn 06/14/11 12:29 P That's a good point there. Thanks PS!Raymond Camden 06/14/11 12:40 P I knew it was something so simple that I could not see it.Michael Dinowitz 06/14/11 12:03 P This should be simple. I have a few radio buttons and when each is clicked, I want a text field to show the sum of the values of the clicked buttons. This should be simple but I can't seem to get it to work. Here's the code I'm using: <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $("input[type=radio]").change(function() { var total = 0; $("input[type=radio]:checked").each(function() { total += $(this).val(); }); $("#totalSum").val(total); }); </script> <form action="radiotest.cfm" method="post"> duss1<br> <input type="radio" name="duss1" value="1" class="duss">1 <input type="radio" name="duss1" value="0" class="duss">0 <br> duss2<br> <input type="radio" name="duss2" value="1" class="duss">1 <input type="radio" name="duss2" value="0" class="duss">0 <br> <input type="text" name="totalSum" id="totalSum" value=""> <input type="submit"> </form> I've tried it using ".duss" rather than "input[type=radio]", .click rather than .change, and "input[name='totalSum']" rather than "#totalSum". What am I doing wrong? Thanks You forgot to wrap it in document.ready. The code fails because nothing matches at the time it is executed. ----- Excess quoted text cut - see Original Post for more ----- You also want to parseInt your values - I'm getting string additions, not numeric additions. ----- Excess quoted text cut - see Original Post for more ----- This worked for me: <script type="text/javascript"> $(document).ready(function() { $("input[type=radio]").change(function() { console.log("ran"); var total = 0; $("input[type=radio]:checked").each(function() { total += parseInt($(this).val()); }); $("#totalSum").val(total); }); }); </script> ----- Excess quoted text cut - see Original Post for more ----- >> total += parseInt($(this).val()); Q: Is there any advantages to using parseint() as opposed to Number()? Thanx G! > parseInt -- Gerald Guido http://www.myinternetisbroken.com "ONE TWO THREE FOUR!!!" -- Dee Dee Ramone Um - no idea. :) ----- Excess quoted text cut - see Original Post for more ----- That was one of my test variables. ParseInt, ParseFloat, no parse function, etc. So that's 2 answers. :) Thanks again > > You also want to parseInt your values - I'm getting string additions, > not numeric additions. > > Note: parseInt(str) will assume octal if the leading digit is a 0. Crockford says to always use parseInt(str,10). On Tue, Jun 14, 2011 at 12:04 PM, Michael Dinowitz < mdinowit@houseoffusion.com> wrote: ----- Excess quoted text cut - see Original Post for more ----- That's a good point there. Thanks PS! ----- Excess quoted text cut - see Original Post for more ----- I knew it was something so simple that I could not see it. Thanks ----- Excess quoted text cut - see Original Post for more -----
|
May 22, 2013
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||