我想获得具有onkeyup事件的文本框的总和。单位乘以等级并将其总和显示到第三个文本框,即a和b,之后我想添加a和b并在总文本框中显示总和。
但它没有在总文本框中显示总和。我认为问题出在onkeyup="s();">
上。
这是我的代码:
function suma(){
var unitsA = document.getElementById('unitsA').value;
var gradesA = document.getElementById('gradesA').value;
var result = parseInt(unitsA) * parseInt(gradesA);
if (!isNaN(result)) {
document.getElementById('a').value = result;
}
}
function sumb(){
var unitsB = document.getElementById('unitsB').value;
var gradeB = document.getElementById('gradeB').value;
var result = parseInt(unitsB) * parseInt(gradeB);
if (!isNaN(result)) {
document.getElementById('b').value = result;
}
}
function s(){
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
var result = parseInt(a) + parseInt(b);
if (!isNaN(result)) {
document.getElementById('total').value = result;
}
}
<input type="text" id="unitsA"/>
<input type="text" id="gradeA" onkeyup="suma();"/>
<input type="text" id="a" name="a" size="7" onkeyup="s();"/>
<input type="text" id="unitsB"/>
<input type="text" id="gradeB" onkeyup="sumb();"/>
<input type="text" id="b" name="b" size="7" onkeyup="s();"/>
<input type="text" id="total" readonly="readonly" />
答案 0 :(得分:1)
您使用了错误的ID名称,此代码有效:
function suma(){
var unitsA = document.getElementById('unitsA').value;
var gradesA = document.getElementById('gradesA').value;
var result = parseInt(unitsA) * parseInt(gradesA);
if (!isNaN(result)) {
document.getElementById('a').value = result;
s();
}
}
function sumb(){
var unitsB = document.getElementById('unitsB').value;
var gradeB = document.getElementById('gradeB').value;
var result = parseInt(unitsB) * parseInt(gradeB);
if (!isNaN(result)) {
document.getElementById('b').value = result;
s();
}
}
function s(){
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
var result = parseInt(a) + parseInt(b);
if (!isNaN(result)) {
document.getElementById('total').value = result;
}
}
<input type="text" id="unitsA"/>
<input type="text" id="gradesA" onkeyup="suma();"/>
<input type="text" id="a" name="a" size="7" onchange="s();"/>
<input type="text" id="unitsB"/>
<input type="text" id="gradeB" onkeyup="sumb();"/>
<input type="text" id="b" name="b" size="7" onchange="s();"/>
<input type="text" id="total" readonly="readonly" />
答案 1 :(得分:1)
您的代码存在一些拼写错误,此处的问题是已解决的解决方案。
function suma() {
var unitsA = document.getElementById('unitsA').value;
var gradesA = document.getElementById('gradeA').value;
var result = parseInt(unitsA) * parseInt(gradesA);
if (!isNaN(result)) {
document.getElementById('a').value = result;
document.getElementById('a').dispatchEvent(new Event('change'));
}
}
function sumb() {
var unitsB = document.getElementById('unitsB').value;
var gradeB = document.getElementById('gradeB').value;
var result = parseInt(unitsB) * parseInt(gradeB);
if (!isNaN(result)) {
document.getElementById('b').value = result;
document.getElementById('b').dispatchEvent(new Event('change'));
}
}
function s() {
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
var result = parseInt(a) + parseInt(b);
if (!isNaN(result)) {
document.getElementById('total').value = result;
}
}
<input type="text" id="unitsA" />
<input type="text" id="gradeA" onkeyup="suma();" />
<input type="text" id="a" name="a" size="7" onchange="s()" />
<input type="text" id="unitsB" />
<input type="text" id="gradeB" onkeyup="sumb();" />
<input type="text" id="b" name="b" size="7" onchange="s()" />
<input type="text" id="total" readonly="readonly" />