我压力很大,因为我不明白为什么我的代码没有在Javascript中显示计算结果。它在另一种语言中,但基本上我只想键入两个变量,如果它们介于它们应该是的值之间,则应该显示文本。这是我的代码,我认为它看起来很好,但不知怎么不起作用。
document.getElementById('calculate').addEventListener('click', function("inwoners") {
var bev = document.getElementById("bevdicht").value;
var opp = document.getElementById("oppervlakte").value;
if (bev < 1 || bev > 700 || opp < 0 || isNaN(bev) || isNaN(opp) || {
div.innerHTML = "Ongeldige waarde ingevoerd!";
return;
}
bev = parseInt(bev); opp = parseInt(opp);
var inwoners = bev * opp;
inwoners = Math.round(inwoners);
if (inwoners > 582000 && inwoners < 585000) {
var text = "Groningen";
}
if (inwoners > 643000 && inwoners < 646000) {
var text = "Friesland";
}
if (inwoners > 48800 && inwoners < 49500) {
var text = "licht overgewicht";
}
if (inwoners > 1130000 && inwoners < 1140000) {
var text = "matig overgewicht";
}
/* if (bmi > 30 && bmi < 40) {
var text = "ernstig overgewicht";
}
if (bmi > 40) {
var text = "ziekelijk overgewicht"; */
}
div.getElementById('calc') innerHTML = "Het aantal inwoners in deze provincie zijn <b>" + inwoners + "</b><p></p> en het is de provincie <b>" + text + "</b>";
}, false);
<form>
De bevolkingsdichtheid is
<input id="bevdicht" size="7" maxlength="5" type="text" placeholder="vul in" />
<p></p>
De oppervlakte van de provincie is
<input id="oppervlakte" size="7" maxlength="9" type="text" placeholder="vul in" />vierkantemeter
<p></p>
<input onclick="inwoners()" type="button" value="Bereken!" />
</form>
<p> </p>
<div id="calc">Het aantal inwoners in deze provincie zijn ..
<p></p>en het is de provincie ...
</div>
如果你能帮我,我会非常感激,也许这是我犯的一个愚蠢的错误
发布于 2015-03-07 00:33:09
你有一些格式化问题。您忽略了第一个if语句中的)
,并且在该if语句中有一个额外的||
。
在那个按钮上也没有id
作为calculate
我翻译了您的代码(因为我不知道它在做什么)。这样做后,并不是很复杂。
见下文,工作代码-
var calcResidents = function() {
var populationDensity = document.getElementById("populationDensity").value;
var provinceArea = document.getElementById("provinceArea").value;
if (populationDensity < 1 || populationDensity > 700 || provinceArea < 0 || isNaN(populationDensity) || isNaN(provinceArea)) {
document.getElementById('calc').innerHTML = "Invalid input value!";
return;
}
var residents = parseInt(populationDensity) * parseInt(provinceArea);
residents = Math.round(residents);
if (residents > 582000 && residents < 585000) {
var text = "Groningen";
}
if (residents > 643000 && residents < 646000) {
var text = "Friesland";
}
if (residents > 48800 && residents < 49500) {
var text = "licht overgewicht";
}
if (residents > 1130000 && residents < 1140000) {
var text = "matig overgewicht";
}
document.getElementById('calc').innerHTML = "The population in this province <b>" + residents + "</b><p></p> and it is the province <b>" + text + "</b>";
};
<form>
The population density is
<input id="populationDensity" size="7" maxlength="5" type="text" placeholder="fill in" />
<p></p>
The area of the province is
<input id="provinceArea" size="7" maxlength="9" type="text" placeholder="fill in" />square meters
<p></p>
<input id="calculate" onclick="calcResidents();" type="button" value="calculated!" />
</form>
<p> </p>
<div id="calc">The population in this province ..
<p></p>and it is the county ...
</div>
密度为500,区域为1166。
希望能帮上忙!
发布于 2015-03-07 00:13:25
您有一些格式化问题:
if
语句在条件后面缺少一个结束括号,并且在后面没有任何内容。https://stackoverflow.com/questions/28909610
复制相似问题