我是编程新手,想练习。
我想制作一个嵌入了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>
谢谢大家。 :)
答案 0 :(得分:1)
您错过.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;
答案 1 :(得分:0)
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>";
第三个变化不一定是技术上的,但它会将每个句子放在自己的行上。