简体   繁体   English

如何让我的代码从 javascript 计算一个值并在 html 中显示答案?

[英]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&sup2)"/> <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.

相关问题 如何使用 javascript 在单独的 html 页面上显示答案? - How do I display an answer on a separate html page using javascript? 单击计算按钮后,javascript 代码不显示未来值的答案 - javascript code doesn't display the answer of future value after I click the calculate button 如何计算两个文本框并在另一个文本框中显示答案? - How do I calculate two text boxes and display the answer in another? 如何使用可从Javascript访问的HTML发送值? - How do I send a value with my HTML that is accessible from Javascript? Javascript 无法获取我的值并在 html 中计算 - Javascript cant get my value and calculate in html 如何获取存储在 javascript 变量中的图像,以显示在 HTML 代码中? - How do I get an image stored in a javascript variable, to display in an HTML code? 您如何执行距离计算并获得答案以在JavaScript中显示? - How do you preform a distance calculation and get answer to display in JavaScript? JavaScript 代码没有回答我的 isset,我该如何解决这个问题? - JavaScript code gives no answer to my isset, how do I solve the problem? 我如何让我的 html 输入元素影响我的 javascript 类对象值(字符名称) - How do i get my html input element to influence my javascript class object value(character name) 如何将单选按钮的值乘以Javascript中的数字,并在文本区域内的文本字符串中显示答案? - How can I multiply a value from a radio button by a number in Javascript and have the answer display in a textstring inside a textarea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM