我需要将数字乘以1到64的值。 我想通过选择给定选择的数字来做得更好,但不知怎的,它只取我选择的第一个值。这有什么不对? 的
<body>
<h1>some calculation</h1>
<script type="text/javascript">
function times(wert) {
var j = document.getElementById('number').value;
return Math.pow(j, wert);
};
</script>
<select id="number" style="margin:40px;">
<option value="1">1=x</option>
<option value="2">2=x</option>
<option value="3">3=x</option>
<option value="4">4=x</option>
</select>
<table border="1mm">
<tbody><tr><th>x</th><th>2*x</th>
<script language="javascript">
for (var i =0; i <= 64; i++) {
out = "<tr><td>" + i + "</td><td>" + times(i)+ "</td></tr>"
document.write(out) ;
}
</script>
</body>
答案 0 :(得分:0)
仅在页面写入时调用times函数一次。尝试将调用该函数的部分封装到“number”节点的onchange事件中。我会将答案附加到表而不是document.write它们。试试这个:
<head>
<script type="text/javascript">
function times(wert) {
var j = document.getElementById('number').value;
return Math.pow(j, wert);
};
function doCalc(){
document.getElementById("output").innerHTML="<tr><th>x</th><th>2*x</th></tr>";
for (var i =0; i <= 64; i++) {
out = "<tr><td>" + i + "</td><td>" + times(i)+ "</td></tr>"
document.getElementById('output').innerHTML += out;
}
}
</script>
<head>
<body>
<select id="number" style="margin:40px;" onchange="doCalc()">
<option value="0">0=x</option>
<option value="1">1=x</option>
<option value="2">2=x</option>
<option value="3">3=x</option>
<option value="4">4=x</option>
</select>
<table border="1mm">
<tbody id="output">
<tr><th>x</th><th>2*x</th></tr>
</tbody>
</table>
</body>