admin管理员组文章数量:1291217
How do I in javascript/asp add two textbox values and display in third? My code is below
function fill() {
var txt8 = document.getElementById("TextBox8").value;
var txt9 = document.getElementById("TextBox9").value;
document.getElementById("TextBox10").value = txt8 + txt9;
}
I have onchange="fill" for both TextBox8 and TextBox9
How do I in javascript/asp add two textbox values and display in third? My code is below
function fill() {
var txt8 = document.getElementById("TextBox8").value;
var txt9 = document.getElementById("TextBox9").value;
document.getElementById("TextBox10").value = txt8 + txt9;
}
I have onchange="fill" for both TextBox8 and TextBox9
Share Improve this question asked Oct 10, 2012 at 21:17 EM90210EM90210 1453 gold badges4 silver badges13 bronze badges5 Answers
Reset to default 1You have to call the function, onchange="fill()"
notice the ()
why don't you use jQUery? Here is a sample:
function fill(){
var total=Number($('#TextBox9').val()) + Number($('#TextBox9').val());
$('#TextBox10').val(total);
}
function Sum() {
var text1 = document.getElementById('<%= TextBox1.ClientID %>');
var text2 = document.getElementById('<%= TextBox2.ClientID %>');
if (text1.value.length == 0 || text2.value.length == 0) {
text1.value = 0;
text2.value = 0;
}
var x = parseFloat(text1.value);
var y = parseFloat(text2.value);
document.getElementById('<%= TextBox3.ClientID %>').value = x + y;
}
<table>
<tr>
<td style="width: 100px">
TextBox1</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" onkeyup="Sum()"></asp:TextBox>
</td>
</tr>
<tr>
<td>
TextBox2</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" onkeyup="Sum()"></asp:TextBox>
</td>
</tr>
<tr>
<td>
TextBox3</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Show" Width="80px" OnClientClick="Sum()" />
</td>
</tr>
</table>
Do you have a set of parentheses following the function call? In javascript if you don't follow a function call with parentheses it assumes you are referencing a variable. Also in the future if you are struggling to debug javascript code you should open up your browsers javascript console to see what is going on and what errors (if any) are thrown.
<input type="text" id="TextBox8" onchange="fill()" />
<input type="text" id="TextBox9" onchange="fill()" />
You need to convert string to integer. value always return string. jsfiddle
<input onchange="fill()" id=TextBox8 />
<input onchange="fill()" id=TextBox9 />
<input id=TextBox10 />
<script>
function fill() {
var txt8 = document.getElementById("TextBox8").value-0;
var txt9 = document.getElementById("TextBox9").value-0;
document.getElementById("TextBox10").value = txt8 + txt9;
}
</script>
本文标签: aspnetjavascript add two textbox values and display in thirdStack Overflow
版权声明:本文标题:asp.net - javascript add two textbox values and display in third - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741516373a2382913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论