简体   繁体   English

如何在JavaScript计算器中显示两个结果?

[英]How can I display two results in a JavaScript calculator?

I am building this little calculator where there is only one input field and when user submits the data, I want form to do 2 calculations and display the results in separate divs. 我正在构建一个只有一个输入字段的小计算器,当用户提交数据时,我希望表单执行2次计算并将结果显示在单独的div中。

In the first div, I want to whatever user puts in the input area and multiply it by 0.017 (result goes into first div). 在第一个div中,我想让任何用户在输入区域中输入并乘以0.017(结果进入第一个div)。 For the second calculation, I want to multiply the whatever value was put into the input field by 0.170 and display the result in a separate div. 对于第二个计算,我想将输入字段中输入的任何值乘以0.170,并将结果显示在单独的div中。 Please see the image for visual rep. 请查看图片以获取视觉代表。

在此处输入图片说明

Essentially you are creating two values (one calculation for each field) then setting the inside of the display elements (the DIVs) to the values of the calculations: 本质上,您要创建两个值(每个字段一个计算),然后将显示元素(DIV)的内部设置为计算值:

var someNumber = 10;
var someNumberAfterCalculationOne = someNumber % 4;
var someNumberAfterCalculationTwo = someNumber - someNumberAfterCalculationOne;

document.getElementById("lt_input").innerHTML = someNumberAfterCalculationOne;
document.getElementById("kg_input").innerHTML = someNumberAfterCalculationTwo;

Here is an example of how you might accomplish this: 这是一个如何完成此操作的示例:

http://jsfiddle.net/nmbwamtd/3/ http://jsfiddle.net/nmbwamtd/3/

This approach is a little ugly, but should get you through to the next step. 这种方法有点难看,但是应该可以带您进行下一步。

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

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