javascript - How to fix this calculate script which has "NaN" in cost box? -
i have calculate script cost, have little problem when try empty price box, there "nan" in cost box.
i not know problem. have made following changes: digitsval=isnan(digitsval)?0:digitsval;
became var digitsval=isnan(digitsval)?0:digitsval;
, first code working, second try empty in cost box, there still "nan" in cost box.
how fix calculate script ?. there solution script?
example :
<!doctype html> <html> <head> <meta charset="utf-8"> <title>berkelilingkesemua.info</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> </head> <body> <script> $(document).ready(function() { var price=$('#price'), phone=$('#phone_number'), digits=$('#digits'), sum=$('#sum'); function calculatesum() { var digitsval=parseint(phone.val().substr(-2)); digitsval=isnan(digitsval)?0:digitsval; digits.val(digitsval); sum.val(parsefloat(price.val())+digitsval); }; phone.on('keyup', calculatesum); price.on('keyup', calculatesum); }); </script> <table width="300px" border="1"> <tr> <td width="40px">1</td> <td>price</td> <td><input class="txt" name="price" type="text" id="price"></td> </tr> <tr> <td>2</td> <td>phone number</td> <td><input name="phone_number" type="text" id="phone_number"></td> </tr> <tr> <td>3</td> <td>2 digits</td> <td><input class="txt" name="digits" type="text" id="digits" readonly></td> </tr> <tr id="summation"> <td> </td> <td align="right">cost :</td> <td align="center"><input type="text" name="sum" id="sum" value="0"></span></td> </tr> </table> </body> </html>
replace:
sum.val(parsefloat(price.val())+digitsval);
with:
isnan(parsefloat(price.val())+digitsval) ? sum.val(0) : sum.val(parsefloat(price.val())+digitsval);
i think should work you
Comments
Post a Comment