<script language="javascript" type="text/javascript">
function sumCalc() {
var _txt1 = document.getElementById('<%= TextBox1.ClientID %>');
var _txt2 = document.getElementById('<%= TextBox2.ClientID %>');
var _txt3 = document.getElementById('<%= TextBox3.ClientID %>');
var _txt4 = document.getElementById('<%= TextBox4.ClientID %>');
var _txt5 = document.getElementById('<%= TextBox5.ClientID %>');
var t1 = 0, t2 = 0, t3 = 0, t4 = 0;
if (txt1.value != "") t1 = txt1.value;
if (txt2.value != "") t2 = txt2.value;
if (txt3.value != "") t3 = txt3.value;
if (txt4.value != "") t4 = txt4.value;
_txt5.value = parseInt(t1) + parseInt(t2) + parseInt(t3) + parseInt(t4) ;
}
</script>
<div>
<asp:TextBox ID=";TextBox2";runat=";服务器";Onchange=";return sumCalc(("gt<asp:TextBox>
<div>
<asp:TextBox ID=";TextBox3";runat=";服务器";Onchange=";return sumCalc(("gt<asp:TextBox>
<div>
<asp:TextBox ID=";TextBox4";runat=";服务器";Onchange=";return sumCalc(("gt<asp:TextBox>
<div> Total:<asp:TextBox ID="TextBox5" runat="server" ></asp:TextBox></div>
在ASP.Net代码中
<div>
<asp:TextBox ID="TextBox1" runat="server" onchange="sumCalc()"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" onchange="sumCalc()"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" onchange="sumCalc()"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" onchange="sumCalc()"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</div>
在Javascript 中
<script type="text/javascript">
function sumCalc()
{
var _txt1 = document.getElementById("TextBox1").value;
var _txt2 = document.getElementById("TextBox2").value;
var _txt3 = document.getElementById("TextBox3").value;
var _txt4 = document.getElementById("TextBox4").value;
if (_txt1 == "")
_txt1 = 0;
if (_txt2 == "")
_txt2 = 0;
if (_txt3 == "")
_txt3 = 0;
if (_txt4 == "")
_txt4 = 0;
var total = parseInt(_txt1) + parseInt(_txt2) + parseInt(_txt3) + parseInt(_txt4);
if (!isNaN(total)) {
document.getElementById('TextBox5').value = total;
}
}
</script>