Use JS to complete a simple calculator function. After you have entered an integer into the 2 input boxes, click on the third input box to give a subtraction of 2 integers.
Tip: Get the value of the element set and get method for: Example: Assignment: document.getElementById ("id"). value = 1; var = document.getElementById ("id"). value;
============== Exercises =================================
The first step: Create the build Operation function count ().
Step two: Get the values in the two input boxes and get the value of the selection box.
Hint: document.getElementById (id name). Value Gets or sets the value of the ID name.
Step three: Get the value selected by the drop-down box to change the subtraction algorithm.
Tip: Use switch to determine the algorithm.
Fourth step: Call the Created function with the = button to get the result.
Note: use the parseint () function to parse a string and return an integer.
The ========================================== code is as follows =============================
<! DOCTYPE html>
<title> Events </title>
<script type= "Text/javascript" >
function count () {
Gets the value of the first input box
var num1=parseint (document.getElementById ("Txt1"). Value);
Gets the value of the second input box
var num2=parseint (document.getElementById ("Txt2"). Value);
Gets the value of the selection box
var Fuhao=document.getelementbyid ("select"). Value;
Gets the value selected by the drop-down box to change the subtraction algorithm
var result;
Switch (Fuhao) {
Case "+":
result=num1+num2;
Break
Case "-":
result=num1-num2;
Break
Case "*":
result=num1*num2;
Break
Case "/":
result=num1/num2;
}
Set the value of the result input box
document.getElementById ("fruit"). Value=result;
}
</script>
<body>
<input type= ' text ' id= ' txt1 '/>
<select id= ' SELECT ' >
<option value= ' + ' >+</option>
<option value= "-" >-</option>
<option value= "*" >*</option>
<option value= "/" >/</option>
</select>
<input type= ' text ' id= ' txt2 '/>
<input type= ' button ' value= ' = ' onclick= ' count () '/> <!--call the created function via the = button to get results--
<input type= ' text ' id= ' fruit '/>
</body>
========================== Interface =========
javascript--Programming Exercise 1