Jump to content

Price Not Being Printed Help Please


lovephp

Recommended Posts

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
    function DisplayPrice(price){
				    var val = 0;
				    for( i = 0; i < document.form.price.length; i++ ){
						    if( document.form.price[i].checked == true ){
								    val = document.form.price[i].value;
						    }
				    }
				    var parseInt(val);
	    document.getElementById('totalSum').value=sum;
    }
   </script>
</head>
<body>
   Choose another number:<br>
   <form name="form2" id="form" runat="server">
    <br>
    <input id="rdo_1" type="radio" value="15" name="price" onclick="DisplayPrice(this.value);">15
    <br>
    <input id="rdo_2" type="radio" value="87" name="price" onclick="DisplayPrice(this.value);">87
    <br>
   </form>
   $<input type="text" name="totalSum" id="totalSum" value="" size="2" readonly="readonly">

</body>
</html>

Link to comment
Share on other sites

You need to go over your code once more I think, as there's some lapses in your logic. If you translate what it does, line-by-line, into plain English you should be able to spot it yourself. Remember to always keep track of what you have, and what has been done, though.

 

PS: Please use proper punctuation so that we don't have to decipher your posts to understand what you're writing. We are, after all, spending our own free time without getting paid a single cent, so by making it as easy as possible for us to help you'll increase your own chances to actually get help.

Link to comment
Share on other sites

Here's an example with checkboxes that will sum the values together in a span tag. It should give you enough of an example for both ways you may want to do it, whether it sums together or not.

 

On a side note. If you're really puttin' much together. JQuery would make your JS quite a bit faster to write, smaller (not counting the include of JQ), and easier to work with.

 

<html>
   <head>
       <script type="text/javascript">
           window.onload = function()
           {
               var btn = window.document.getElementsByClassName("checkbox");

               for (var i in btn)
               {
                   if (btn[i].value != undefined)
                   {
                       btn[i].checked = 0;

                       btn[i].onclick = function()
                       {
                           var total = 0;

                           for (var x in btn)
                           {
                               if (btn[x].checked != undefined)
                               {
                                   if (btn[x].checked == 1)
                                   {
                                       total += parseInt(btn[x].value * 100);
                                   }

                                   window.document.getElementById("totalSum").innerHTML = '$' + Math.round(total) / 100;
                               }
                           }
                       }
                   }
               }
           }
       </script>
   </head>
   <body>
       Choose another number:<br /><br />

       <form name="form2" id="form" method="post" action="#">
           <input class="checkbox" type="checkbox" value="15.30" name="price" />15.30<br />
           <input class="checkbox" type="checkbox" value="87.24" name="price" />87.24<br />
           <input class="checkbox" type="checkbox" value="13.278" name="price" />13.278<br />
           <input class="checkbox" type="checkbox" value="82.964" name="price" />82.964<br />
       </form>

       <b>Total:</b> <span id="totalSum">$0</span>
   </body>
</html>

Link to comment
Share on other sites

Not sure if recreating the function for each element is bad or not, so you may want to just set the onclick with a loop to the function. In JQuery you would just use $(".class").click(function() { }); which I assume does the same as I did above, but I'll let someone else comment on whether that's wrong to do or not. :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.