jquery - Pass a javascript variable through a hidden input -
okay, have javascript calculate dynamic price html form. code follows:
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); });
this, input:
<input class="irrelevant typeahead" type="text" placeholder="amount" name="amount"/>
the javascript takes value, calculates price, , want fill this:
<input type="hidden" name="price" value=""/>
i believe javascript correct. need price make work?
make sure have these items wrapped in <form>
tag.
<form> <input class="irrelevant typeahead" type="text" placeholder="amount" name="amount"/> <br /> <input type="hidden" name="price" value=""/> </form> 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); });
i have working demo here: http://jsfiddle.net/p55ge/
in demo have made price input text field instead of hidden field ease of seeing value being set.
also note, field updated when enter valid number , click off field input. following quote jquery documentation .change()
http://api.jquery.com/change/
now when second option selected dropdown, alert displayed. displayed if change text in field , click away. if field loses focus without contents having changed, though, event not triggered.
Comments
Post a Comment