javascript - Form calculator using Each() and Children() in jQuery -


I am trying to create a form that calculates the total value based on string values ​​such as a series of drop down boxes It's like "this option costs £ 30" I know this is not ideal but IM is putting together an existing script as a hack

For most part Iive is working on it But I'm not sure that each function for each function How to not run child of #productconfig

I can input every drop down id manually into an array and it does the calculation, but it would be nice if it comes with all children of #productconfig Works

> pre & lt; & Lt; Div id = "# productconfig" & gt; & Lt; Label & gt; Model type & lt; / Labels & gt; & Lt; Select Name = "Product [220] [Data] [Model Type]" ID = "Data-Model-Type-220" & gt; & Lt; Option value = "M-type £ 500" & gt; M-Type £ 500 & lt; / Options & gt; & Lt; Option value = "P-type £ 500" & gt; P-Type £ 500 & lt; / Options & gt; & Lt; Option value = "S-type £ 500" & gt; S-Type £ 500 & lt; / Options & gt; & Lt; / Select & gt; & Lt; / Div & gt; & Lt; / Code & gt; & Lt; Code & gt; $ (Document) .ready (function () {$ ("# productconfig"). Children () .changes (function () {calculateoptions ();}); calculateoptions ();}); & Lt; / Code & gt; & Lt; Code & gt; Function calculation () {var arr = ["data-model type-20"]; Var Total = 0; JQuery.each (arr, function () {var str = $ ('#' + this) .attr ("value"); var poundsign = str.indexOf ('£'); var poundsign = poundsign + 1; var lengthofstr = Str.length; var shortstr = str.substr (poundsign, lengthofstr); total = eval (total) + eval (shortstr);}); $ ('# Value') Html ("£" + total); } & Lt; / Code & gt;

How about:

  function calculations Options () {Var total = 0; Jquery ('# productconfig select') each (function () {total + = $ (this) .val (). Match (/ £ (\ d +) /) [1];}); $ ('# Value') Html ("£" + total); }  

Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -