php - Can I change dyanmic javascript variable depending on user form input? -


my question here if there way change dynamic price calculation depending on name user enters in input field.

i have code works calculating dynamic price in html form. here code:

using input:

<input class="typeahead" type="text" placeholder="amount" name="amount"/> 

my javascript calculates price:

jquery("input[name='amount']").change(function () { if (isnan(parsefloat(this.value)) || !isfinite(this.value)) {     jquery(this).val('');     return false; } var calc = parsefloat(this.value) * 0.95; jquery(this).parents("form").find("input[name='price']").val(calc); }); 

that works fantastic. calculates amount .95 , assigns value price.

if add form:

<input class="stores typeahead" type="text" placeholder="stores" name="name"/> 

what can here able calculate price @ different values depending on store name. example, if enters mcdonalds, want calculate @ .90. if enters target, want calculate @ .92. previous javascript cannot accomplish because calculates @ .95 instead of being able change depending on store entered.

i prefer accomplish javascript because i'm not skilled php.

you can create javascript object this.

var stores = {      "mcdonalds" : .90,      "target" : .92, } var storename = jquery(this).parents("form").find("input[name='name']").val(); console.log(stores[storename]); //output store cost console. 

though think jquery lookup function questionable. i'm sure there better way select textbox. that's general concept you're looking for.


Comments

Popular posts from this blog

Unable to open serial port using Putty to bluetooth device on windows 7 64 bit -

bash - How do you programmatically add a bats test? -

java - Getting exception in JDBC thin driver -