我有一个简单的代码,可以添加两个数字。结果显示在文本框中。如何在div中显示结果,而不是文本框?
这是我当前的代码,它在文本框中显示结果。
function sum() {
var field1 = document.getElementById('txt1').value;
var field2 = document.getElementById('txt2').value;
var field1V = parseInt(field1);
var field2V = parseInt(field2);
var result = field1V + field2V;
if (!isNaN(result)) {
document.getElementById('txt3').value = result;
}
}
<input type="text" placeholder="Type number" id="txt1" onkeyup="sum();" />
<input type="text" placeholder="Type number" id="txt2" onkeyup="sum();" />
Result: <input type="text" id="txt3" readonly />
答案 0 :(得分:2)
function sum() {
var field1 = document.getElementById('txt1').value;
var field2 = document.getElementById('txt2').value;
var field1V = parseInt(field1);
var field2V = parseInt(field2);
var result = field1V + field2V;
if (!isNaN(result)) {
document.getElementById('txt3').value = result;
document.getElementById('result').innerHTML = result;
}
}
&#13;
<input type="text" placeholder="Type number" id="txt1" onkeyup="sum();" />
<input type="text" placeholder="Type number" id="txt2" onkeyup="sum();" />
Result: <input type="text" id="txt3" readonly />
Result: <div id='result'></div>
&#13;