0

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

If Page.IsPostBack Then

        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())
4

1 回答 1

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/

于 2012-11-16T11:28:30.220 回答