首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何显示多个用户输入

如何显示多个用户输入
EN

Stack Overflow用户
提问于 2019-10-17 21:02:16
回答 1查看 312关注 0票数 0

我正在尝试获取用户输入并将其显示给用户,但它在用户输入后继续重置,例如,从1000开始,然后输入10现金,结果是990,但在您再次输入20之后,它没有保存您输入的990,而是重新开始并输出980

你可以明白我在这里的意思

https://codepen.io/scottajames/pen/mddreXz

代码语言:javascript
复制
//  Hides everything in the Content-2 Div-
document.getElementById("Content-2").hidden = true;

// Unhides everything in the content-2 div and hides everything in the content-1 div and saves the input of the monthly id 
function FirstButton() {
  var a = document.getElementById("Monthly").value;
  document.getElementById("Monthly-Amount").innerHTML = a;
  document.getElementById("Content-2").hidden = false;
  document.getElementById("Content-1").hidden = true;
}
// adds the content-2 inputs together and takes away the price from the total
function SecondButton() {
  var b = document.getElementById("Item").value;
  var c = document.getElementById("Price").value;
  document.getElementById("Item-Price").innerHTML = b + ' ' + c;

  var d = document.getElementById("Monthly").value;
  document.getElementById("Monthly-Amount").innerHTML = d - c;
}
代码语言:javascript
复制
body {
  background: #34EBC6;
}

#Content {
  margin: 0 auto;
  position: relative;
  top: 200px;
  width: 500px;
}

input {
  width: 350px;
  height: 60px;
  outline: none;
  font-size: 24px;
  padding-left: 30px;
  display: inline-block;
  border-radius: 5px;
  border: none;
  background: #0e8b72;
  color: white;
}

::placeholder {
  color: white;
}

button {
  position: relative;
  height: 60px;
  width: 100px;
  font-size: 25px;
  border-radius: 5px;
  border: none;
  color: white;
  background: #0e8b72;
}

#Content-1 h1 {
  position: relative;
  left: 100px;
  color: white;
  font-size: 25px;
  font-family: sans-serif;
  font-weight: 400;
}

#Content-2 h1 {
  color: white;
  font-size: 25px;
  font-family: sans-serif;
  font-weight: 400;
}

#Monthly-Amount {
  position: relative;
  top: -10px;
  left: 150px;
  font-size: 40px;
  font-family: sans-serif;
  color: white;
  font-weight: 400;
}

#Item-Price {
  position: relative;
  top: 50px;
  border-top: 1px solid black;
  padding-top: 25px;
  padding-left: 25px;
}
代码语言:javascript
复制
<div id="Content">

  <div id="Content-1">
    <h1>Monthly Budget </h1><input type="text" id="Monthly" placeholder="0.00" required>
    <button id="Button-1" onclick="FirstButton()">Next</button>
  </div>

  <h1 id="Monthly-Amount"></h1>


  <div id="Content-2">


    <h1>Item: </h1><input type="text" id="Item" placeholder="Item" required>
    <h1>Price: </h1><input type="text" id="Price" placeholder="Price" required>
    <button id="Button-2" onclick="SecondButton()">Next</button>

    <h1 id="Item-Price"></h1>
  </div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-18 17:09:45

以防以后有人遇到同样的问题,您所要做的就是添加一个+=,而不仅仅是=作为输出

从…

代码语言:javascript
复制
document.getElementById("Item-Price").innerHTML = b +' '+ c;

代码语言:javascript
复制
document.getElementById("Item-Price").innerHTML += b +' '+ c;

代码语言:javascript
复制
  var d = document.getElementById("Monthly").value;
  document.getElementById("Monthly-Amount").innerHTML = d - c;

被更改为

代码语言:javascript
复制
document.getElementById("Monthly-Amount").innerHTML -=  c;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58433327

复制
相关文章

相似问题

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