在Asp.Net中使用Java脚本自动计算文本框 [英] Autocalculate Textboxes using Java script in Asp.Net
问题描述
大家好,我遇到了这个问题.我想要做的就是在文本框之间进行一些数学运算,并将结果显示在另一个文本框内.操作包括加法,减法和百分比计算.我使用JavaScript编写了此代码,但无法使其自动在 TextBox1
中显示计算结果(添加 TotalFeesPaidTextbox
的结果和 AmountReceivedTextbox
),当用户将光标/突出显示放在 TextBox1
上时.请看看并帮助我;我很感激.
Hey guys, I am stuck with this problem. All I want is to do some mathematical operations among textboxes and display that result in another textbox. Operations include addition, subtraction and % calculation. I started with this code using JavaScript, but could not make it automatically display the calculated result in TextBox1
(result of addition of TotalFeesPaidTextbox
and AmountReceivedTextbox
) when user put cursor/highlights on TextBox1
. Please have a look and help me; I appreciate it.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="Application.WebForm7" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script language="javascript" type="text/javascript">
function sumCalc() {
var _txt1 = document.getElementById('<%= TotalFeesPaidTextBox.ClientID %>');
var _txt2 = document.getElementById('<%= AmountReceivedTextbox.ClientID %>');
var _txt3 = document.getElementById('<%= TextBox1.ClientID %>');
var t1 = 0, t2 = 0;
if (_txt1.value != "") t1 = _txt1.value;
if (_txt2.value != "") t2 = _txt2.value;
_txt3.value = parseInt(t1) + parseInt(t2);
}
</script>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:TextBox ID="TotalFeesPaidTextBox" runat="server">
<asp:TextBox ID="AmountReceivedTextbox" runat="server" onkeyup ="sumCalc" AutoPostBack="True">
<asp:TextBox ID="TextBox1" runat="server">
推荐答案
调用您的javascript函数onblur,这样当光标离开AmountReceivedTextBox它将调用该函数.
call your javascript function onblur so when cursor leave AmountReceivedTextBox it will call the function.
<asp:textbox id="AmountReceivedTextbox" runat="server" onblur="javascript:sumCalc();" xmlns:asp="#unknown">
</asp:textbox>
这篇关于在Asp.Net中使用Java脚本自动计算文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!