Javascript 根据表单输入打印乘法表-不打印结果
我正在尝试计算和打印乘法表的结果 表单接受数字以及乘法的开始和结束范围 结果应以“12 x 1=12”的格式打印,以此类推 除了最后一行在“content”div中打印计算结果外,所有操作都正常 结果只闪烁了一秒钟。没有错误 HTMLJavascript 根据表单输入打印乘法表-不打印结果,javascript,Javascript,我正在尝试计算和打印乘法表的结果 表单接受数字以及乘法的开始和结束范围 结果应以“12 x 1=12”的格式打印,以此类推 除了最后一行在“content”div中打印计算结果外,所有操作都正常 结果只闪烁了一秒钟。没有错误 HTML 输入号码: 从:到: Javascript 函数时间表(){ Num=document.getElementById(“myNum”).value; startValue=document.getElementById(“sValue”).value; en
输入号码:
从:到:
Javascript
函数时间表(){
Num=document.getElementById(“myNum”).value;
startValue=document.getElementById(“sValue”).value;
endValue=document.getElementById(“eValue”).value;
var结果=[];
for(var i=startValue;i您需要在for循环之后插入DOM
函数时间表(){
总数=”;
Num=document.getElementById(“myNum”).value;
startValue=document.getElementById(“sValue”).value;
endValue=document.getElementById(“eValue”).value;
var结果=[];
for(var i=startValue;i您需要在for循环之后插入DOM
函数时间表(){
总数=”;
Num=document.getElementById(“myNum”).value;
startValue=document.getElementById(“sValue”).value;
endValue=document.getElementById(“eValue”).value;
var结果=[];
对于(var i=startValue;i而言,这是典型的闭包问题。它显示了最后输入的值
将代码更改为以下内容
函数时间表(){
Num=document.getElementById(“myNum”).value;
startValue=document.getElementById(“sValue”).value;
endValue=document.getElementById(“eValue”).value;
var结果=[];
var content=document.getElementById(“内容”);
var-res=[];
对于(var i=startValue;i而言,这是典型的闭包问题。它显示了最后输入的值
将代码更改为以下内容
函数时间表(){
Num=document.getElementById(“myNum”).value;
startValue=document.getElementById(“sValue”).value;
endValue=document.getElementById(“eValue”).value;
var结果=[];
var content=document.getElementById(“内容”);
var-res=[];
对于(var i=startValue;i它正在工作,但当我单击按钮时,我看到输出闪烁一秒钟,然后再次清空。知道为什么会发生这种情况吗?在你的情况下还是在我的情况下?你和Geekys的解决方案都给出了相同的问题。代码正在工作,但结果不会保留在屏幕上。哦,这是因为当你单击提交按钮时表单已提交,您将看到空白页。为了防止出现这种情况,您可以使用onsubmit=“return false;”
i have edited:)的内联属性它可以工作,但当我点击按钮时,我看到输出闪烁了一秒钟,然后再次清空。你知道为什么会发生这种情况吗?在你的情况下还是在我的情况下?你和Geekys的解决方案都给出了相同的问题。代码可以工作,但结果不会保留在屏幕上。哦,那是因为当你点击提交按钮时。表单被提交并您将看到空白页。为了防止出现这种情况,您可以使用onsubmit=“return false;”
i have edited:)的内联属性
<form name="multiply">
<p>Enter the number: <input type="number" id="myNum"></p>
<p>From: <input type="number" id="sValue"> To: <input type="number" id="eValue"></p>
<input type="submit" name="Calculate" onclick="timesTable()">
</form>
<div id="content"></div>
function timesTable() {
Num = document.getElementById("myNum").value;
startValue = document.getElementById("sValue").value;
endValue = document.getElementById("eValue").value;
var result = [];
for (var i = startValue; i <= endValue; i++) {
result[i] = Num * i;
document.getElementById("content").innerHTML = Num + " x " + i + " = " + result[i] + "<br>";
}
}