在文本框中添加数字

时间:2012-11-15 23:06:02

标签: addition

我有7个文本框。我想添加值7并将总数插入另一个文本框。如何在没有回发的情况下实现这一目标。我也尝试更新面板,但它无法正常工作。我的代码如下。在这方面的任何建议都会非常明显

如果Page.IsPostBack那么

        Dim A1 As Double
        A1 = A_SAtxt.Text
        Dim A2 As Double
        A2 = A_SStxt.Text
        Dim A3 As Double
        A3 = A_RAtxt.Text
        Dim A4 As Double
        A4 = A_CAtxt.Text
        Dim A5 As Double
        A5 = A_OItxt.Text
        Dim A6 As Double
        A6 = A_ICtxt.Text
        Dim A7 As Double
        A7 = A_Otxt.Text


        Dim ATotal = A1 + A2 + A3 + A4 + A5 + A6 + A7

结束如果

我使用VB在asp.net中工作。 asp.net中的文本框代码如下

                                        

                        <table width="100%">
                            <tr>
                                <td align="left" style="border:1px solid #cccccc; text-        align: right; background-color: #F0F0F0;" 
                                    valign="top" class="style7" width = "400px">
                                    <asp:Label ID="Label154" runat="server" Text="Savings account $"></asp:Label>
                                </td>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: left; background-color: #F0F0F0;" 
                                    valign="top">

                                    <asp:TextBox ID="A_SAtxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" MaxLength="25"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: right; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:Label ID="Label155" runat="server" Text="Stock and securities $"></asp:Label>
                                </td>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: left; background-color: #F0F0F0;" 
                                    valign="top">

                                    <asp:TextBox ID="A_SStxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" MaxLength="25"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: right; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:Label ID="Label156" runat="server" Text="Retirement accounts $"></asp:Label>
                                </td>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: left; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:TextBox ID="A_RAtxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" MaxLength="25"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: right; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:Label ID="Label157" runat="server" Text="Checking account $"></asp:Label>
                                </td>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: left; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:TextBox ID="A_CAtxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" MaxLength="25"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: right; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:Label ID="Label158" runat="server" Text="Other investments $"></asp:Label>
                                </td>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: left; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:TextBox ID="A_OItxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" MaxLength="25"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: right; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:Label ID="Label159" runat="server" Text="Insurace cash-in-value $"></asp:Label>
                                </td>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: left; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:TextBox ID="A_ICtxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" MaxLength="25"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: right; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:Label ID="Label160" runat="server" Text="Other $"></asp:Label>
                                </td>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: left; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:TextBox ID="A_Otxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" MaxLength="30"></asp:TextBox>
                                    <asp:Label ID="Label188" runat="server" Text="specify:" Font-Italic="true"></asp:Label>
                                    <asp:TextBox ID="A_OStxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" Width="250px" MaxLength="100"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: right; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:Label ID="Label161" runat="server" Text="Total assets $"></asp:Label>
                                </td>
                                <td align="center" class="style7" 
                                    style="border:1px solid #cccccc; text-align: left; background-color: #F0F0F0;" 
                                    valign="top">
                                    <asp:TextBox ID="A_TAtxt" runat="server" CssClass="DrpItems10" 
                                        style="text-align: left" MaxLength="25"></asp:TextBox>
                                </td>
                            </tr>
                        </table>
                         </ContentTemplate>
                                </asp:UpdatePanel>

我甚至尝试过这样做,但似乎没有效果

        Dim sb As New System.Text.StringBuilder()
        sb.Append("<script type = 'text/javascript'>")
        sb.Append(" $(document).ready(function() {")
        sb.Append(" var numberInput = $('#A_SAtxt, #A_SStxt, #A_RAtxt, #A_CAtxt,                 

                #A_OItxt, #A_ICtxt, #A_Otxt');")
        sb.Append("  numberInput.on('change', function() {")
        sb.Append(" var total = 0;")
        sb.Append("else{")
        sb.Append(" numberInput.each(function() {")
        sb.Append("  if ($(this).val() !== '') {")
        sb.Append("  total += parseFloat($(this).val());")
        sb.Append(" }")
        sb.Append(" });")
        sb.Append(" $('#A_TAtxt').val(total);")
        sb.Append("  });")
        sb.Append(" });​")

        sb.Append("}};</script>")
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "function", sb.ToString())

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery或javascript计算7个输入框的总和,并将值插入客户端的另一个输入。

<强> JAVASCRIPT

var total = 0;

$('#A_SAtxt, #A_SStxt, #A_RAtxt, #A_CAtxt, #A_OItxt, #A_ICtxt, #A_Otxt').each(function() {
    if ($(this).val() !== '') {
        total += parseFloat($(this).val());
    }
});

$('#A_TAtxt').val(total);

<强>样本

以下是此代码的实例示例:
http://jsfiddle.net/AWGsw/2/

* 编辑1 *

要在页面加载时调用此函数并立即在输入模糊上添加值,请执行以下操作:

<强> JAVASCRIPT

$(document).ready(function() {

    var numberInput = $('#A_SAtxt, #A_SStxt, #A_RAtxt, #A_CAtxt, #A_OItxt, #A_ICtxt, #A_Otxt');

    numberInput.on('change', function() {
        var total = 0;

        numberInput.each(function() {
            if ($(this).val() !== '') {
                total += parseFloat($(this).val());
            }
        });

        $('#A_TAtxt').val(total);
    });

});

<强>样本

http://jsfiddle.net/AWGsw/4/