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. Effect as above:
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.
Code:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title>Event</title> 5 <Scripttype= "Text/javascript">6 functioncount () {7 vara=document.getElementById ("txt1"). Value;8 varb=document.getElementById ("txt2"). Value;9 //gets the value of the first input boxTen //gets the value of the second input box One //gets the value of the selection box A varC=document.getElementById ("Select"). Value; - a=parsefloat (a); - b=parsefloat (b); the //gets the value selected by the drop-down box to change the subtraction algorithm - //set the value of the result input box - Switch(c) { - Case "+": + document.getElementById ("Fruit"). Value=parseint (a)+parseint (b); - Break; + Case "-": A document.getElementById ("Fruit"). Value=parseint (a)-parseint (b); at Break; - Case "*": - Ocument.getelementbyid ("Fruit"). Value=parseint (a)*parseint (b); - Break; - Case "/": - document.getElementById ("Fruit"). Value=parseint (a)/parseint (b); in Break; - } to } + </Script> - </Head> the <Body> * <inputtype= ' text 'ID= ' Txt1 '/> $ <SelectID= ' SELECT '>Panax Notoginseng <optionvalue="+">+</option> - <optionvalue="-">-</option> the <optionvalue="*">*</option> + <optionvalue="/">/</option> A </Select> the <inputtype= ' text 'ID= ' txt2 '/> + <inputtype= ' button 'value= ' = 'onclick= "count ()"/> - <inputtype= ' text 'ID= ' Fruit '/> $ </Body> $ </HTML>
JS to implement a simple calculator