Implementation of the 9.JavaScript simple calculator

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.