Home  >  Article  >  Web Front-end  >  JS operation numerical calculation in input tag

JS operation numerical calculation in input tag

php中世界最好的语言
php中世界最好的语言Original
2018-04-19 10:21:172369browse

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(&#39;put1&#39;);
var oIput2=document.getElementById(&#39;put2&#39;);
var nAdd=document.getElementById(&#39;add&#39;);
var nSub=document.getElementById(&#39;subtraction&#39;);
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn