打印给定的句子,给定的时间,使用javascript

Print a given sentence , given times, using javascript

本文关键字:javascript 使用 句子 打印 时间      更新时间:2023-09-26

我是编程新手,想练习。我想制作一个嵌入javascript的html文档,该文档在一个字段中有两个输入字段,用户将键入句子,在第二个字段中,她将键入她想要打印的次数,而不是当她单击按钮时,脚本将在"printhere"div中打印该文本。
我尝试过但失败了,以下是我的代码,有人可以告诉我我在搞砸什么吗?

<html>
<body>
<!-- form started -->
<form>
<!-- what sentence to print -->
What to write?<br>
<input id="what"><br><br>
<!-- how many times to print -->
How many times ?<br>
<input id="howmany"><br><br>
<!-- the button -->
<button type="button" onclick="printItNow()">Print Now</button>
<form>
<!-- form ended -->
<!-- the div where the content will print -->
<div id="printhere">
</div>

<script>
function printItNow() {
    var printhere = document.getElementById('printhere');
    var whatto = document.getElementById('what');
    var howmany = document.getElementById('howmany');

    for (var i=0; i<howmany; i++) {
        printhere.innerHTML += whatto;
    }
}
</script>
</body>
<html>

谢谢大家,:)

你错过了.value,在document.getElementById()中,更改:

var whatto = document.getElementById('what');
var howmany = document.getElementById('howmany');

var whatto = document.getElementById('what').value;
var howmany = document.getElementById('howmany').value;
Change 
var whatto = document.getElementById('what');
to 
var whatto = document.getElementById('what').value;

var howmany = document.getElementById('howmany');
to 
var howmany = document.getElementById('howmany').value;
and
printhere.innerHTML += whatto;
to
printhere.innerHTML += whatto+"<br>";

第三个变化在技术上不是必须的,但它会把每个句子都放在自己的行上。