Home >Web Front-end >JS Tutorial >JS operation numerical calculation in input tag
This time I bring to you, what are the precautions, the following is a practical case, let's take a look.
<!DOCTYPE html> <html> <head> <metacharset="UTF-8"> <title></title> <script> window.onload=function(){ var oIput1=document.getElementById('put1'); var oIput2=document.getElementById('put2'); var nAdd=document.getElementById('add'); var nSub=document.getElementById('subtraction'); nAdd.onclick=function(){ var a=Number(oIput1.value); var b=Number(oIput2.value); c = a+b; alert(c); } nSub.onclick=function(){ var a=Number(oIput1.value); var b=Number(oIput2.value); c = a-b; alert(c); } } </script> </head> <body> 数值1:<inputid="put1"type="text"value=""/> 数值2:<inputid="put2"type="text"value=""/> <inputtype="button"id="add"value="求和"/> <inputtype="button"id="subtraction"value="相减"/> </body> </html>
# I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Real-time acquisition of input input box content
Detailed explanation of the use of Vue scope slots
vue implements the function of clicking on the image to enlarge (with code)
The above is the detailed content of JS operation numerical calculation in input tag. For more information, please follow other related articles on the PHP Chinese website!