想要在javascript中添加两个文本框值,以便当我离开第二个文本框时,第三个将给出前两个的附加值 [英] Want to add two textbox value in javascript, So that when I leave 2nd textbox , 3rd will give added value of first two
问题描述
这是我的javascript代码:-
This is my javascript code:-
<script language="javascript" type="text/javascript">
function functn() {
var a = document.getElementById('TextBox1').value;
var b = document.getElementById('TextBox2').value;
if (a == "")
a = 0;
else if (b == "")
b = 0;
var res = parseInt(a) + parseInt(b);
if (!NaN(res)) {
document.getElementById('TextBox3').value = res;
}
else
document.getElementById('TextBox3').value = 0;
}
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
无法弄清楚我错了.需要帮助!
Can''t figure out where I''m wrong. Need HELP!!
推荐答案
您的编码中有几件不好的事情.
There is several bad thing in your coding.
function functn() {
//no problem
var a = document.getElementById('TextBox1').value;
//no problem
var b = document.getElementById('TextBox2').value;
if (a == "")
a = 0;
//problem
//why:
//if a=="" then next statement do not get verified
//what to do? : remove else
else if (b == "")
b = 0;
//no problem
var res = parseInt(a) + parseInt(b);
//problem;
//big problem;
//if you check on debugger ">typeof NaN", you will get "number"; NaN is not a function. you are suppose to check isNaN; your problem will be solved, I guess.
if (!NaN(res)) {
document.getElementById('TextBox3').value = res;
}
else
document.getElementById('TextBox3').value = 0;
//other than the one compilation error and one logical error everything seems fine.
}
如何调试:我使用三种软件来调试与网络相关的问题
1. Firefox:(ctrl + shift + k)
2. Chrome:(ctrl + shift + j)
3. Internet Explorer(11);偶尔(F12)
因此,打开,例如说Chrome,按ctrl + shift + j
然后在新打开的控制台中编写functn()
按Enter
您会看到函数错误;
无论如何,给它一个机会.
How to debug: I use three software to debug web related issues
1. Firefox : (ctrl+shift+k)
2. Chrome : (ctrl+shift+j)
3. Internet Explorer(11); occasionally(F12)
so, open, say Chrome, press ctrl+shift+j
and then write, in the newly opened console, functn()
press enter
you will see your function error;
anyway, give it a shot.
所以我找到了一个按钮单击事件的解决方案:-
So I found a solution with the button click event:-
<script language="javascript" type="text/javascript">
function functn() {
var a = document.getElementById('TextBox1').value;
var b = document.getElementById('TextBox2').value;
if (a == "")
a = 0;
if (b == "")
b = 0;
var res = parseInt(a) + parseInt(b);
//if (!NaN(res))
{
document.getElementById('TextBox3').value = res;
}
}
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<button id="Button7" value="Click" onclick="functn()">Click</button>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
但是我不需要按钮.
我想要的是,一旦离开第二个文本框,第三个文本框就会自动获得总和.
But I want no buttons.
What I want is, as soon as I leave the 2nd textbox, the 3rd textbox gets the sum automatically.
这篇关于想要在javascript中添加两个文本框值,以便当我离开第二个文本框时,第三个将给出前两个的附加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!