Example of "About JavaScript" auto-calculation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.