Implementation of Multiple input summation in jQuery
This article mainly introduces jQuery's implementation of multiple input Summation Methods, involving jQuery's skills in getting input form element values and operations. For more information, see
This example describes how to implement multiple input summation of jQuery. Share it with you for your reference. The specific implementation method is as follows:
The html page code is as follows:
?
1 2 3 4 5 6 7 8 9 |
<Td> <Input name = "add" id = "add" readonly = "readonly"/> </Td> <Pre name = "code" class = "html"> <td> <Input name = "add1" id = "add1"/> </Td> <Td> <Input name = "add2" id = "add2"/> </Td> |
Some jQuery code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Script> $ ("Input [id ^ = 'add']"). change (function (){ Var sum = 0; $ ("Input [id ^ = 'add']"). each (function (){ Var r =/^ -? \ D + $/; // positive integer If ($ (this). val ()! = ''&&! R. test ($ (this). val ())){ $ (This). val (""); // The regular expression is not null. } Else if ($ (this). val ()! = ''){ Sum + = parseInt ($ (this). val ()); } Document. getElementById ("add"). value = sum; }); }); </Script> |
Because the input attribute is readonly, when you press Backspace in the browser to delete the input value, the page will return a response. For solutions, refer to the previous article JQuery implementation of methods to prevent return of the return Space key.
I hope this article will help you with jQuery programming.