enter qty and update total order
Just making a very very simple order form.
Want a input box ( only accept numbers )
User enters a quant开发者_如何学运维ity , lets say 50
We have a multiplier value which is lets say 10
Want a disabled form field, showing the result of 50 x 10
So form field would show 500
so now we have variable orderTotal we can pass to our code like:
$txtAmount = "orderTotal";
Any ideas ?
Code must be somewhat similar. but not tested. if you find error then please let me know
EDIT:: the tested code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
<script>
$(function (){
$("#value").blur(
function(){
var value = $(this).val();
if(isInt(value)){
$("#total").val($(this).val()*$("#quantity").val());
}
else{
alert('pelase enter int');
}
}
);
$('#quantity').change(
function()
{
var value =$("#value").val();
if(isInt(value))
{
if($("#quantity").val() > 0){
$("#total").val($(this).val()*$("#value").val());
}
}
else{
alert('pelase enter int');
}
}
)
});
function isInt(x) {
var y=parseInt(x);
if (isNaN(y)) return false;
return x==y && x.toString()==y.toString();
}
</script>
<form>
<input id="value" name="value" />
<select id="quantity">
<option>10</option>
<option>20</option>
<option>30</option>
<option>40</option>
<option>50</option>
</select>
</form>
<input id="total" type="text" disabled="disabled"/>
update:: this should work but not sure .. if not working then let me know
@explorex has you most of the way there, but one thing I'd be cautious about is floating point math in Javascript is not precise. If you're dealing with money... you want to make sure you're precise.
Here's a SO question with answers about this: https://stackoverflow.com/questions/744099/javascript-bigdecimal-library
精彩评论