abdelelbouhy Posted August 9, 2010 Share Posted August 9, 2010 hello guysin this script i want to get the total of the form field and display it to the customer after selecting his order but what happen it return it as string next each other not the total of them here is the code$(function(){$('div div').hide();var total = 0; $(':checkbox').click(function(){ var checked = this.checked; $('div',$(this).parents('div:first')).css('display',checked ? 'block':'none'); $('input[type=text]',$(this).parents('div:first')).attr('disabled',!checked).css('color',checked? '#000':'red').val(1).change().each(function(){ if (!checked) {$('input[type=text] ~span',$(this).parents('div:first')).text('')}; if(checked) this.focus(); $('input[type=text] ~span').each(function(){ total = total + $(this).text(); //here is the problem $('#total').text(total); }) }); }); $('input[type=text]').change(function(){ $('~span',this).text($(this).val() * $(this).parents('span[price]').attr.('price')); }); }); </script> </head> <body> <h1>Bamboo Asian Grille</h1> <h2>Online Order Menu</h2> <fieldset> <legend>Appetizers</legend> <div> <label> <input type="checkbox" name="appetizers" value="imperial"/> Fried Imperial rolls (2) </label> <span price="3"> <input type="text" name="imperial.quantity" disabled="disabled" value="1"/> $<span></span> </span> <div> <label> <input type="radio" name="imperial.option" value="pork" checked="checked"/> Pork </label> <label> <input type="radio" name="imperial.option" value="vegetarian"/> Vegetarian </label> </div> </div> <div> <label> <input type="checkbox" name="appetizers" value="spring"/> Spring rolls (2) </label> <span price="4"> <input type="text" name="spring.quantity" disabled="disabled" value="1"/> $<span></span> </span> <div> <label> <input type="radio" name="spring.option" value="pork" checked="checked"/> Pork </label> <label> <input type="radio" name="spring.option" value="shrimp"/> Pork and Shrimp </label> <label> <input type="radio" name="spring.option" value="vegetarian"/> Vegetarian </label> </div> </div> <div> <label> <input type="checkbox" name="appetizers" value="vnrolls"/> Vietnamese rolls (4) </label> <span price="5"> <input type="text" name="vnrolls.quantity" disabled="disabled" value="1"/> $<span></span> </span> <div> <label> <input type="radio" name="vnrolls.option" value="pork" checked="checked"/> Pork </label> <label> <input type="radio" name="vnrolls.option" value="shrimp"/> Pork and Shrimp </label> <input type="radio" name="vnrolls.option" value="vegetarian"/> <label>Vegetarian</label> </div> </div> <div> <label> <input type="checkbox" name="appetizers" value="rangoon"/> Crab rangoon (8) </label> <span price="6"> <input type="text" name="rangoon.quantity" disabled="disabled" value="1"/> $<span></span> </span> <div> <label> <input type="radio" name="rangoon.option" value="sweet checked="checked"/> Sweet-and-sour sauce </label> <label> <input type="radio" name="rangoon.option" value="hot"/> Hot mustard </label> <label> <input type="radio" name="rangoon.option" value="both"/> Both </label> </div> </div> <div> <label> <input type="checkbox" name="appetizers" value="stickers"/> Pot stickers (6) </label> <span price="5"> <input type="text" name="stickers.quantity" disabled="disabled" value="1"/> $<span></span> </span> <div> <label> <input type="radio" name="stickers.option" value="pork" checked="checked"/> Pork </label> <label> <input type="radio" name="stickers.option" value="vegetarian"/> Vegetarian </label> </div> </div> <div></div> </fieldset> <div id="total">a</div> </body></html> Link to comment Share on other sites More sharing options...
Ingolme Posted August 9, 2010 Share Posted August 9, 2010 You can cast it as a number in order to make it do a sum: total = Number($(this).text()) + total; Link to comment Share on other sites More sharing options...
abdelelbouhy Posted August 9, 2010 Author Share Posted August 9, 2010 You can cast it as a number in order to make it do a sum:total = Number($(this).text()) + total; thank you very much Ingolme finally works Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.