In some trade business web systems, some pages need to provide real-time auxiliary computing capabilities, such as the value of the unit price and quantity of the goods entered by the employee, and the total price can be displayed directly through JavaScript event processing.
The operation of this example is as follows:
In this case, digital validation is also used, and if the user does not enter reasonable data in the text box, the system pops up a warning dialog box similar to the following.
In this example, a parameterless function price_total () is defined as the KeyUp event handler for both unit price and quantity.
In addition, we have defined a public function total (Price,amount) that contains two parameters to calculate the overall price.
Here is the key JS code implementation section:
1<script language= "JavaScript" >2 functionTotal (price,amount) {3 varTotalprice=parseint (Amount) *parsefloat (price);4Totalprice=math.round (totalprice*100)/100;5Document.form1.totalprice.value=Totalprice;6 }7 functionprice_total () {8Document.form1.totalprice.value= "";9 varAmount=Document.form1.amount.value;Ten varPrice=document.form1.price.value;if(IsNaN (Price)) { OneAlert (the unit price must be a number!) "); A Document.form1.price.focus (); - Document.form1.price.select (); - return false;} the if(IsNaN (amount)) { -Alert ("Number must be a number!") "); - Document.form1.amount.focus (); - Document.form1.amount.select (); + return false;} - if(price!= "" &&amount!= "") + Total (price,amount);} A</script>
Example of "About JavaScript" auto-calculation