需要帮助获取使用Javascript显示的输出吗

需要帮助获取使用Javascript显示的输出吗,javascript,Javascript,我需要在表格下方的范围内显示两个数字的输出(和、差、积、商)。我尝试了许多变化,但根本无法得到总和等显示。请帮帮我,我是个傻瓜!!!谢谢 function calculate() { 'use strict'; // Read the first text box and convert to a float var input = document.getElementById('number1').value; var number1 = parseInt(input); var i

我需要在表格下方的范围内显示两个数字的输出(和、差、积、商)。我尝试了许多变化,但根本无法得到总和等显示。请帮帮我,我是个傻瓜!!!谢谢

 function calculate() {
'use strict';

// Read the first text box and convert to a float
var input = document.getElementById('number1').value;
var number1 = parseInt(input);


var input = document.getElementById('number2').value;
var number2 = parseInt(input);


// Calculations 
sum = number1 + number2;
difference = number1 - number2;
product = number1 * number2;
quotient = number1 / number2;

// Display the results in relevant spans
document.getElementById('sum').value = sum;
document.getElementById('difference').value = difference;
document.getElementById('product').value = product;
document.getElementById('quotient').value = quotient;


return false;

} 


function init() {
'use strict';
    document.getElementById('theForm').onsubmit = calculate;
} 
window.onload = init;
HTML


第一
二号






要为span标记赋值,我们需要使用
innerHTML
属性,而不是
value

document.getElementById('sum').innerHTML = sum;
document.getElementById('difference').innerHTML = difference;
document.getElementById('product').innerHTML = product;
document.getElementById('quotient').innerHTML = quotient;
改变

<form action="" method="post" id="theForm">



您只需在表单提交时调用
calculate
函数。

就快到了!您要求4个目标div的
.value
属性-问题是,div没有
.value
属性。改用innerHTML。例如:
document.getElementById('sum')。innerHTML=sum谢谢!!!innerHTML做到了!不过,它只在Firefox中有效。你知道为什么吗?如果它在chrome上不起作用,打开页面,按F12键,看看它是否显示任何错误。你太棒了!谢谢@DEDE请考虑投票,并接受它作为答案,如果它真的帮助你
<form action="" method="post" id="theForm">
<form action="" method="post" id="theForm" onsubmit="return calculate()">