首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用JavaScript添加、减去或乘两个变量?

如何使用JavaScript添加、减去或乘两个变量?
EN

Stack Overflow用户
提问于 2016-05-21 04:58:45
回答 1查看 4.4K关注 0票数 0
代码语言:javascript
复制
<!DOCTYPE html>
<head><title> test ! </title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
</head>
<html>
<body>

<div class="container">

first number : <input class="form-control" id="num1" />
<br>
second number : <input class="form-control" id="num2" />
<br>
<select id="mathtype">
   <option value="add"> addition </option>
   <option value="sub"> subtraction </option>
   <option value="mul"> multiplication </option>
</select>
<br><br>
<button class="btn btn-primary" onclick="submit()" > submit </button>
<br><br>
<input type="text" id="output" >

</div>



</body>
<script src="js/jquery-1.9.1.js"></script>
<script>

function submit(){

    var mathtype = document.getElementById['mathtype'];

    if (mathtype == add ) {
        return num1 + num2;
    } else if (mathtype == sub ) {
        return num1 - num2;
    } else if (mathtype == mul ) {
        return num1 * num2;
    }   

    return true;

}

</script>

</html>

我当前的错误:

SyntaxError:函数语句需要一个名称。

我想要制作一个程序,它将根据所选的值(add,sub,mul)执行一个数学操作,在单击submit之后,答案将显示在输入中,其中有一个id "output“。

EN

回答 1

Stack Overflow用户

发布于 2016-05-21 05:02:09

代码语言:javascript
复制
function submit() {
  var mathtype = document.getElementById('mathtype').value;
  //Get the value of the select element..Correct the typo([])
  var num1 = Number(document.getElementById('num1').value);
  //Get the value of `num1` input and convert it to type `Number`
  var num2 = Number(document.getElementById('num2').value);
  //Get the value of `num2` input and convert it to type `Number`
  if (mathtype == 'add') {
    //Compare with `string` as value of select input will be of type string, you did not have variable `add` to be tested
    document.getElementById('output').value = num1 + num2;
  } else if (mathtype == 'sub') {
    //Compare with `string` as value of select input will be of type string, you did not have variable `sub` to be tested
    document.getElementById('output').value = num1 - num2;
  } else if (mathtype == 'mul') {
    //Compare with `string` as value of select input will be of type string, you did not have variable `mul` to be tested
    document.getElementById('output').value = num1 * num2;
  }
}
代码语言:javascript
复制
<div class="container">
  first number :
  <input class="form-control" id="num1" />
  <br>second number :
  <input class="form-control" id="num2" />
  <br>
  <select id="mathtype">
    <option value="add">addition</option>
    <option value="sub">subtraction</option>
    <option value="mul">multiplication</option>
  </select>
  <br>
  <br>
  <button class="btn btn-primary" onclick="submit()">submit</button>
  <br>
  <br>
  <input type="text" id="output">
</div>

注意事项__:详细解释请参阅评论

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37359066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档