gpt4 book ai didi

javascript - HTML 和 JavaScript,javascript 没有输出,我不知道为什么?

转载 作者:行者123 更新时间:2023-12-03 09:23:47 25 4
gpt4 key购买 nike

总的来说,HTML5 新手很抱歉,如果这是一个简单的修复:)

我有这个 HTML 文件,它需要 4 个用户输入(整数)并将它们乘以 4 个其他变量,然后将新变量相加并通过 document.getElementById("out ").innerHTML 当我在 Chrome 中运行 HTML 时,它什么也不做,我不知道为什么,Adobe Dreamweaver 根本没有标记任何错误,而且据我所知,它在浏览器中运行良好。如果您确实修复了它,请尝试解释一下您在做什么以及为什么它有效,并尝试保持我的代码的形式,以便我仍然可以理解它(希望这是有意义的?)

这是我的完整代码-

<!doctype html>
<html>
<body>
<h1><font face="arial">How much space do you need?</font></h1>
<h2><font face="arial">Number of Files: </font></h2>

<form id="filenum">
<font face="arial"> Documents: <input type="int" name="doc" value="0"><br></font>
<font face="arial"> Photos: <input type="int" name="photo" value="0"><br></font>
<font face="arial"> Music: <input type="int" name="music" value="0"><br> </font>
<font face="arial"> Films: <input type="int" name="film" value="0"><br></font>
</form>
<button onclick="outputsize()">Calculate</button>

<script>
var doc,photo,music,film,ttb,tgb,tmb,tdoc,tphoto,tmusic,tfilm,files,sdoc,sphoto,smusic,sfilm;

function outputsize() {
sdoc=0.1
sphoto=8
smusic=5
sfilm=1400
files=document.getElementById("filenum");
doc=x.elements["doc"].value;
photo=x.elements["photo"].value;
music=x.elements["music"].value;
film=x.elements["film"].value;
tdoc=doc*sdoc;
tphoto=photo*sphoto;
tmusic=music*smusic;
tfilm=film*sfilm;
tmb=tdoc + tphoto + tmusic + tfilm;
tgb=tmb/1000;
ttb=tgb/1000;
document.getElementById("out").innerHTML="You need a, "+tmb+"MB or bigger hard drive.<br>";
document.getElementById("out").innerHTML+="Or...<br>";
document.getElementById("out").innerHTML+="You need a, "+tgb+"GB or bigger hard drive.<br>";
document.getElementById("out").innerHTML+="Or...<br>";
document.getElementById("out").innerHTML+="You need a, "+ttb+"TB or bigger hard drive.<br>";
}
</script>

<p id="out"><font face="arial">We calculated:</font></p>

</body>
</html>

再次,抱歉,如果这是一个愚蠢的问题(如果存在这样的事情),但我已经研究了几个小时但无法解决它:(

最佳答案

这应该可以正常工作。您试图从表单中获取值,但您试图从不存在的变量中获取它们。

<h1><font face="arial">How much space do you need?</font></h1>


<h2><font face="arial">Number of Files: </font></h2>

<form id="filenum">
<font face="arial"> Documents: <input type="int" name="doc" value="0"><br></font>

<font face="arial"> Photos: <input type="int" name="photo" value="0"><br></font>

<font face="arial"> Music: <input type="int" name="music" value="0"><br> </font>

<font face="arial"> Films: <input type="int" name="film" value="0"><br></font>

</form>
<button onclick="outputsize()">Calculate</button>
<script>
var doc, photo, music, film, ttb, tgb, tmb, tdoc, tphoto, tmusic, tfilm, files, sdoc, sphoto, smusic, sfilm;

function outputsize() {
sdoc = 0.1;
sphoto = 8;
smusic = 5;
sfilm = 1400;
files = document.getElementById("filenum");
doc = files.elements["doc"].value;
photo = files.elements["photo"].value;
music = files.elements["music"].value;
film = files.elements["film"].value;
tdoc = doc * sdoc;
tphoto = photo * sphoto;
tmusic = music * smusic;
tfilm = film * sfilm;
tmb = tdoc + tphoto + tmusic + tfilm;
tgb = tmb / 1000;
ttb = tgb / 1000;
document.getElementById("out").innerHTML = "You need a, " + tmb + "MB or bigger hard drive.<br>";
document.getElementById("out").innerHTML += "Or...<br>";
document.getElementById("out").innerHTML += "You need a, " + tgb + "GB or bigger hard drive.<br>";
document.getElementById("out").innerHTML += "Or...<br>";
document.getElementById("out").innerHTML += "You need a, " + ttb + "TB or bigger hard drive.<br>";
}
</script>
<p id="out"><font face="arial">We calculated:</font>
</p>

关于javascript - HTML 和 JavaScript,javascript 没有输出,我不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31754116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com