简体   繁体   English

如何获取多个用户文本输入值,将它们与 javascript 相加并返回页面上的值<p></p>

[英]How to take multiple user text input values, add them together with javascript and return the value on page in a <p>

I am very new to Javascript and am looking for a solution to better understand how this works.我是 Javascript 的新手,正在寻找一种解决方案以更好地了解其工作原理。

I am making a simple budget app with various user text inputs.我正在制作一个带有各种用户文本输入的简单预算应用程序。 What I want to accomplish is to store these user inputs inside variables within JS, have them added, possibly compared, then return a total on the page when the user clicks a button in the empty paragraph element.我想要完成的是将这些用户输入存储在 JS 中的变量中,将它们添加,可能进行比较,然后当用户单击空段落元素中的按钮时在页面上返回总计。 Here is a sample of the html code (I have only included two of the inputs as they will work the same and just need an understanding of the concept):这是 html 代码的示例(我只包含了两个输入,因为它们的工作原理相同,只需要了解这个概念):

<ul>
            <li><p>Money In:</p></li>
            <br>
            <li class="inputHeading">Current Balance</li>
            <li><input type="text" class="input" id="balanceInput"></li>
            <li><p>Money Out:</p></li>
            <br>
            <li class="inputHeading">Rent</li>
            <li><input type="text" class="input" id="rentInput"></li>
            <li><input type="button" class="button" value="AMOUNT LEFT" onclick="calcTotal()"></li>
            <li class="output" id="amountOutput"><p></p></li>
</ul>

I have researched various methods but most of what I find is far more complex than I can currently understand.我研究了各种方法,但我发现的大部分方法都比我目前所能理解的要复杂得多。 I wanted to see if anyone could both suggest and explain a solution to how to do this.我想看看是否有人可以建议和解释如何执行此操作的解决方案。 Your help would be much appreciated!非常感谢您的帮助!

You can store the input in js with document.getElementById("id").value您可以使用 document.getElementById("id").value 将输入存储在 js 中
However this will be stored in string, so you need to change it into integer/float if you want to calculate the total但是这会存储在字符串中,所以如果你想计算总数,你需要把它变成整数/浮点数
parseFloat() is the method to parse string text into float values. parseFloat() 是将字符串文本解析为浮点值的方法。
In the example you provided, the values is current balance and rent, hence I think it is more suitable to find the difference instead of adding them together.在您提供的示例中,值为当前余额和租金,因此我认为找出差异而不是将它们相加更合适。

 function calcTotal() { var inputbal = parseFloat(document.getElementById("balanceInput").value); var inputrent = parseFloat(document.getElementById("rentInput").value); var balance = inputbal - inputrent; document.getElementById("balance").innerHTML = balance.toFixed(2); }
 <ul> <li> <p>Money In:</p> </li> <br> <li class="inputHeading">Current Balance</li> <li><input type="text" class="input" id="balanceInput"></li> <li> <p>Money Out:</p> </li> <br> <li class="inputHeading">Rent</li> <li><input type="text" class="input" id="rentInput"></li> <li><input type="button" class="button" value="AMOUNT LEFT" onclick="calcTotal()"></li> <li class="output" id="amountOutput"> <p></p> </li> </ul> <p id="balance"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM