1. Difficulty, how to get the value of the tag, note that the obtained value is a string type, but also to convert
var num1 = parseint (document.getElementById ("NUM1"). Value);
var num2 = parseint (document.getElementById ("num2"). Value);
2. How to get the value under the Select drop-down menu:
Here said my own Baidu came.
var tag = document.getElementById ("Myselect");
alert (tag);
Get the value of a label
var opeartor = Tag.options[tag.selectedindex].value;
Implementation code:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Calculator implementation</title> <Scripttype= "Text/javascript"> functionOpera () {varNUM1=parseint (document.getElementById ("NUM1"). Value); varnum2=parseint (document.getElementById ("num2"). Value); varTag=document.getElementById ("Myselect"); //alert (tag); //get the value of a label varOpeartor=Tag.options[tag.selectedindex].value; //alert (opeartor); //alert (NUM1); //alert (num2); //declaring variables to store values varresult= 0; Switch(opeartor) { Case "+": Result=NUM1+num2; //alert (' Test '); Break; Case "-": Result=NUM1-num2; //alert (' msg '); Break; Case "*": Result=NUM1*num2; Break; Case "/": Result=NUM1/num2; Break; } //alert (result); //alert (' msg ');
Assigns a value to the label of id= "result"document.getElementById ("result"). Value=result; } </Script></Head><Body> <H1>Calculator implementation</H1> <inputtype= "text"ID= "NUM1" /> <SelectID= "Myselect"> <optionvalue="+"ID="+">+</option> <optionvalue="-"ID="-">-</option> <optionvalue="*"ID="*">*</option> <optionvalue="/"ID="/">/</option> </Select> <inputtype= "text"ID= "Num2" /> = <inputtype= "text"ID= "Result"onclick= "Opera ()"></Body></HTML>
Implementation of the 9.JavaScript simple calculator