问题描述
我分配了一个添加两个文本框值的任务.我希望添加结果在第三个文本框中出现,只要在前两个文本框中输入值,而无需按任何按钮即可.
I have assigned a task to add two textbox values.I want the result of addition to appear in the 3rd textbox,as soon as enter the values in the first two textboxes,without pressing any buttons.
例如:在第一个文本框中,我想输入450,当我按下数字"450"的数字4时,它将被添加到第三个文本框中,在前两个文本框中按下的任何数字突然改变将会反映在第三个文本框中.我该怎么做?
For eg:In the first textbox i want to enter 450,when i press digit 4 of number '450',then it will be added to the 3rd textbox,any number i press in the first two textboxes,suddenly that changes will be reflected on the third textbox.How can i do this?
在这里,我在onkeyup中编写我的代码调用sum()
Here i write my code call sum() in onkeyup
onkeyup="sum()"
function sum() {
var txtFirstNumberValue = document.getElementById('txt1').value;
var txtSecondNumberValue = document.getElementById('txt2').value;
var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
if(!isNaN(result)){
document.getElementById('txt3').value = result;
}
}
这不适用于Chrome浏览器
This is not working in chrome
推荐答案
尝试一下
function sum() {
var txtFirstNumberValue = document.getElementById('txt1').value;
var txtSecondNumberValue = document.getElementById('txt2').value;
if (txtFirstNumberValue == "")
txtFirstNumberValue = 0;
if (txtSecondNumberValue == "")
txtSecondNumberValue = 0;
var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
if (!isNaN(result)) {
document.getElementById('txt3').value = result;
}
}
这篇关于添加两个文本框值,并自动在第三个文本框中显示总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!