【发布时间】:2019-07-17 09:33:23
【问题描述】:
当输入为 2 时打印 '2 x 10 = 20' 但不是整个表。我尝试了各种方法。但结果是一样的。
没有错误。就像打印整个乘法表一样。
function loop() {
var i = 1;
var x = document.getElementById("num").value;
//document.getElementById("result").value = result;
while (i <= 10) {
document.getElementById("result").value = x + " x " + i + " = " + i * x;
i++;
}
}
<h1>While loop: </h1>
<p>The while loop keeps repeating an action until an associated condition returns false.</p>
<img src="images/loop.jpg" /><br/>
<img src="images/loop2.jpg" /><br/>
<body>
<p>JavaScripts Runs:</p>
<script src="while_1loop.js">
</script><br/> What number table would you like to have?<input type="number" name="" id="num" /><br>
<button type="button" onclick="loop()" ;>Start</button><br>
<textarea rows="12" cols="15" id="result" readonly>
</textarea><br/>
【问题讨论】:
-
朋友们,我看到的循环函数来自于源码while_2loop.js。 :)
-
document.getElementById("result").value += x + " x " + i + " = " + (i * x)+"\n"; -
这个问题是由无法再复制的问题或简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者。这通常可以通过在发布之前确定并仔细检查重现问题所需的最短程序来避免。
-
当然,但这仍然是一个骗局,如果这是一个问题,他们可以查看Javascript: filling inputs with text without overwriting the text that is already there
标签: javascript