Note the followingCodeTo load jquery, please download the latest version from the official website.
Copy code The Code is as follows: <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> JavaScript operations on the gridview for automatic calculation </title>
<Style type = "text/CSS"> <! --
Table, TR, TD {text-align: center ;}
Input {width: 50px; text-align: center ;}
--> </Style> <style type = "text/CSS" bogus = "1"> table, TR, TD {text-align: center ;}
Input {width: 50px; text-align: center ;}
</Style>
<SCRIPT type = "text/JavaScript" src = "http://img.jb51.net/jslib/jquery/jquery-1.2.6.pack.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript"> <! --
// Global
VaR TBL;
// Change the total amount and Total Quantity
Function settotal (){
VaR totalamount = 0; // total amount
VaR totalcount = 0; // total number
If (TBL! = NULL & TBL. Rows. length> 2) // the header occupies a row.
{
For (VAR n = 1; n <TBL. Rows. Length-1; n ++) // The Rows array starts from 0 and occupies a row.
{
// Total Quantity
If (! Isnan (TBL. Rows [N]. cells [2]. childnodes [0]. Value ))
{
Totalcount + = Number (TBL. Rows [N]. cells [2]. childnodes [0]. value );
}
// Total amount
If (! Isnan (TBL. Rows [N]. cells [3]. innertext) // determines if it is a number.
{
Totalamount + = Number (TBL. Rows [N]. cells [3]. innertext );
}
}
}
TBL. Rows [TBL. Rows. Length-1]. cells [2]. innertext = totalcount;
TBL. Rows [TBL. Rows. Length-1]. cells [3]. innertext = totalamount;
}
// The unit price is changed. Find the quantity and amount of the same row based on the row number,
// You can bind these values with Index = '<% # container. dataitemindex %>'
Function fprice (rowid, Val ){
TBL. Rows [number (rowid)]. cells [3]. innertext =
Number (VAL) * Number (TBL. Rows [number (rowid)]. cells [2]. childnodes [0]. value );
}
// Quantity change
Function fcount (rowid, Val ){
TBL. Rows [number (rowid)]. cells [3]. innertext =
Number (VAL) * Number (TBL. Rows [number (rowid)]. cells [1]. childnodes [0]. value );
}
// Only numbers can be entered
Function checknum ()
{
If (event. keycode> = 48 & event. keycode <= 57) | event. keycode = 8 | (event. keycode> = 96 & event. keycode <= 105)
| Event. keycode = 46 | event. keycode = 37 | event. keycode = 39 | event. keycode = 190 | event. keycode = 110)
{
Event. returnvalue = true;
}
Else
{
Event. returnvalue = false;
}
}
Jquery (function (){
// Initialize the table
// TBL = Document. getelementbyid ("gridview1 ");
TBL = $ ("# gridview1"). Get (0); // return the DOM object
// Input Keyboard restrictions
Jquery ("input"). keydown (function (){
Checknum ();
}). Keyup (function (){
Settotal ();
});
});
// -->
| NO. |
unit price |
quantity |
amount |
| 1 |
|
|
|
| 2 |
|
|
|
| total |
|