[英]How do I get my code to calculate a value from javascript and display the answer in html?
I am trying to create code something that calculates the ddf (daily water flow) from a household based on the type of household, number of bedrooms, number of occupants, and floor area.我正在尝试创建代码,根据家庭类型、卧室数量、居住人数和建筑面积计算家庭的 ddf(每日水流量)。
Right now I have completed the code for a single family with any number of bedrooms and occupants (no code for floor area yet and other types of homes).现在我已经完成了具有任意数量卧室和居住者的单个家庭的代码(尚无建筑面积和其他类型房屋的代码)。 But it is not showing the answer or calculating it.
但它没有显示答案或计算它。
The if
statements are based on guidelines for calculating water flow based on the info. if
语句基于根据信息计算水流量的准则。
bedrooms=document.getElementsById("bedrooms").innerHTML;
In order to get the input value, you should use .value为了获得输入值,您应该使用.value
Also, you are using a single =
as your operator, which will always return true.此外,您使用单个
=
作为您的运算符,它将始终返回 true。 Should be if(residence == "single")
.应该是
if(residence == "single")
。
It also looks like you are trying to get an element that doesn't exist in order to set the information.看起来您还试图获取一个不存在的元素以设置信息。 Try creating a tag with the Id
ddf
such as <p id=\\'ddf\\'><\\/p>
.尝试使用 Id
ddf
创建标签,例如<p id=\\'ddf\\'><\\/p>
。
You also have some typos in your code you should check.您的代码中也有一些拼写错误,您应该检查一下。 Many of these errors could be found with the console fairly quickly.
使用控制台可以很快找到其中的许多错误。 You should get familiar with the Chrome Developer Console .
您应该熟悉Chrome 开发者控制台。
So, you've got some issues with your code.所以,你的代码有一些问题。
(residence = single)
returns error cause single is undefined. (residence = single)
返回错误原因 single 未定义。
You didn't create an element with ddf
id.您没有使用
ddf
id 创建元素。
The way to get value from inputs is .value()
and not innerHTML
.从输入中获取值的方法是
.value()
而不是innerHTML
。
You have typo's with getElementsById
, should be getElementById
.你有
getElementsById
错字,应该是getElementById
。
function calcDDF() { var ddf=0; let bedrooms=document.getElementById("bedrooms").value; let occupants=document.getElementById("occupants").value; let residence=document.getElementById("residence").value; if (residence == 'single') { if (bedrooms == 1 && occupants == 2 || occupants == null) { ddf=700; } if (bedrooms == 1 && occupants > 2) { ddf=occupants*350; } if (bedrooms == 2 && occupants == 3 || occupants == null) { ddf=1000; } if (bedrooms == 2 && occupants > 3) { ddf=occupants*350; } if (bedrooms == 3 && occupants == 3.75 || occupants == null) { ddf=1300; } if (bedrooms == 3 && occupants > 3.75) { ddf=occupants*350; } if (bedrooms == 4 && occupantss == bedrooms + 0.5 || occupants == null) { ddf=1600+((bedrooms-4)*300); } if (bedrooms => 4 && occupants > bedrooms + 0.5) { ddf=occupants*350; } } document.getElementById("ddf").innerHTML = ddf; }
<input type="number" id="bedrooms" placeholder="Number of Bedrooms" /> <input type="number" id="occupants" placeholder="Number of Occupants" /> <input type="number" id="area" placeholder="Floor Area (m²)"/> <select name="type of residence" id="residence"> <option value=""disabled selected>Select the Type of Residence</option> <option value="single">Single Family Dwelling</option> <option value="multi">Multi Family</option> <option value="luxury">Luxury Home</option> <option value="cottage">Seasonal Cottage</option> <option value="mobile">Mobile Home</option> </select> <button onclick="calcDDF()">Calculate DDF</button> <br> <p id="ddf"></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.