Javascript 计算和回发

发布于 2024-08-26 07:12:29 字数 9247 浏览 6 评论 0原文

我正在开发一个 ASP.NET Web 应用程序,其中使用向导引导客户端完成一系列步骤。

其中一个步骤包括即时计算一堆数字...数字计算正确,但当我单击“下一步”然后再次返回时...某些数字不会保留。

这是计算函数,

function CalculateFields() {

    txtSellingPrice = document.getElementById('<%=txtSellingPrice.ClientID %>');
    //I get all of the elements like this...
    //...   

    var regexp = /[$,]/g;


    //Empty value checks
    SellingPrice = (SellingPrice == "" ? "$0" : SellingPrice);
    BalanceSheet = (BalanceSheet == "" ? "$0" : BalanceSheet);
    DownPayment = (DownPayment == "" ? "$0" : DownPayment);
    susearn = (susearn == "" ? "$0" : susearn);
    susrev = (susrev == "" ? "$0" : susrev);
    balmult = (balmult == "" ? "$0" : balmult);
    goodmult = (goodmult == "" ? "$0" : goodmult);
    sellmult = (sellmult == "" ? "$0" : sellmult);


    //Replace $ with String.Empty
    SellingPrice = txtSellingPrice.value.replace(regexp, "");
    BalanceSheet = txtBalanceSheet.value.replace(regexp, "");
    DownPayment = txtDownPayment.value.replace(regexp, "");
    susearn = txtSusEarn.value.replace(regexp, "");
    susrev = txtSusRev.value.replace(regexp, "");
    balmult = txtBalanceMult.value.replace(regexp, "");
    goodmult = txtGoodwillMult.value.replace(regexp, "");
    sellmult = txtSellingPriceMult.value.replace(regexp, "");


    //Set the new values
    txtGoodWill.value = "$" + (SellingPrice - BalanceSheet);
    txtBalance.value = "$" + (SellingPrice - DownPayment);
    txtSellingPriceMult.value = "$" + SellingPrice;
    txtGoodwillMult.value = "$" + (SellingPrice - BalanceSheet);
    txtBalanceMult.value = "$" + BalanceSheet;

    if (chkTakeBack.checked == 1) {
        txtVendorTakeBackPercentage.value = Math.round((SellingPrice - DownPayment) / SellingPrice * 100);
    }
    else {
        txtVendorTakeBackPercentage.value = "0";
    }

    if (!(susearn == "") && !(susearn == "0") && !(susearn == "$0")) {
        txtSusEarnPercentage.value = Math.round(susearn / susrev * 100);
        txtBalanceMultPercentage.value = Math.round(balmult / susearn);
        txtGoodwillMultPercentage.value = Math.round(goodmult / susearn);
        txtSellingPriceMultPercentage.value = Math.round(sellmult / susearn);
    }
    else {
        txtSusEarnPercentage.value = "0";
        txtBalanceMultPercentage.value = "0";
        txtGoodwillMultPercentage.value = "0";
        txtSellingPriceMultPercentage.value = "0";
    }

}

所有这些都可以正确计算并在回发中保留其值,

    txtGoodWill.value = "$" + (SellingPrice - BalanceSheet);
    txtBalance.value = "$" + (SellingPrice - DownPayment);
    txtSellingPriceMult.value = "$" + SellingPrice;
    txtGoodwillMult.value = "$" + (SellingPrice - BalanceSheet);
    txtBalanceMult.value = "$" + BalanceSheet;

但是这些函数不会在回发中保留其值

        if (chkTakeBack.checked == 1) {
            txtVendorTakeBackPercentage.value = Math.round((SellingPrice - DownPayment) / SellingPrice * 100);
        }
        else {
            txtVendorTakeBackPercentage.value = "0";
        }

        if (!(susearn == "") && !(susearn == "0") && !(susearn == "$0")) {
            txtSusEarnPercentage.value = Math.round(susearn / susrev * 100);
            txtBalanceMultPercentage.value = Math.round(balmult / susearn);
            txtGoodwillMultPercentage.value = Math.round(goodmult / susearn);
            txtSellingPriceMultPercentage.value = Math.round(sellmult / susearn);
        }
        else {
            txtSusEarnPercentage.value = "0";
            txtBalanceMultPercentage.value = "0";
            txtGoodwillMultPercentage.value = "0";
            txtSellingPriceMultPercentage.value = "0";
        }

txtVendorTakeBackPercentage 总是返回空白 其他三个总是以 0 返回,

我通过在表单字段中使用 onkeyup 事件来触发这些函数。

    If Not Page.IsPostBack Then
        txtSellingPrice.Attributes.Add("onkeyup", "CalculateFields()")
        txtBalanceSheet.Attributes.Add("onkeyup", "CalculateFields()")
        txtDownPayment.Attributes.Add("onkeyup", "CalculateFields()")
        txtSusRev.Attributes.Add("onkeyup", "CalculateFields()")
        txtSusEarn.Attributes.Add("onkeyup", "CalculateFields()")
    End If

任何想法/帮助/方向将不胜感激。

编辑:
我的页面加载事件看起来像这样...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load


    'Prepair page for advanced validation'
    Dim cs As ClientScriptManager = Page.ClientScript
    If Not cs.IsOnSubmitStatementRegistered(Me.[GetType](), "PrepareValidationSummary") Then _
        cs.RegisterOnSubmitStatement(Me.[GetType](), "PrepareValidationSummary", "CheckForm()")

    If Not Page.IsPostBack Then

        'Part of the client side math'
        txtSellingPrice.Attributes.Add("onkeyup", "CalculateFields()")
        txtBalanceSheet.Attributes.Add("onkeyup", "CalculateFields()")
        txtDownPayment.Attributes.Add("onkeyup", "CalculateFields()")
        txtSusRev.Attributes.Add("onkeyup", "CalculateFields()")
        txtSusEarn.Attributes.Add("onkeyup", "CalculateFields()")

我的实际 aspx 页面有超过 2400 行,所以我不打算发布整个内容。这是百分比字段不保留其值的部分。

  <tr>
                        <td colspan="5" class="AlignRight">
                            Average Annual Sustainable Earnings (ebitda) estimated&nbsp;
                            <ew:NumericBox ID="txtSusEarnPercentage" runat="server" Enabled="False" MaxLength="3"
                                Width="20px" TruncateLeadingZeros="True" />
                            %
                        </td>
                        <td class="AlignRight">
                            <ew:NumericBox ID="txtSusEarn" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
                                MaxLength="9" Width="92px" />
                        </td>
                        <td>
                            <asp:Image ID="HelpEBITDA0" runat="server" Height="16px" ImageUrl="~/Images/Icons/help.gif" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" class="AlignRight">
                            &nbsp;
                        </td>
                        <td class="AlignRight">
                            &nbsp;
                        </td>
                        <td>
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" class="AlignRight">
                            Balance Sheet / Net Asset Value as earnings multiple&nbsp;&nbsp;
                            <ew:NumericBox ID="txtBalanceMultPercentage" runat="server" Enabled="False" MaxLength="3"
                                Width="20px" />
                        </td>
                        <td class="AlignRight">
                            <ew:NumericBox ID="txtBalanceMult" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
                                MaxLength="9" Width="92px" Enabled="False" />
                        </td>
                        <td>
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" class="AlignRight">
                            Goodwill Value as earnings multiple&nbsp;&nbsp;
                            <ew:NumericBox ID="txtGoodwillMultPercentage" runat="server" Enabled="False" MaxLength="3"
                                Width="20px" />
                        </td>
                        <td class="AlignRight">
                            <ew:NumericBox ID="txtGoodwillMult" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
                                MaxLength="9" Width="92px" Enabled="False" />
                        </td>
                        <td>
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" class="AlignRight">
                            Proposed Selling Price as earnings multiple&nbsp;&nbsp;
                            <ew:NumericBox ID="txtSellingPriceMultPercentage" runat="server" Enabled="False"
                                MaxLength="3" Width="20px" />
                        </td>
                        <td class="AlignRight">
                            <ew:NumericBox ID="txtSellingPriceMult" runat="server" AutoFormatCurrency="True"
                                DecimalPlaces="2" MaxLength="9" Width="92px" Enabled="False" />
                        </td>
                        <td>
                            &nbsp;
                        </td>
                    </tr>

这是我填写时的表格
before.png

这是我点击“下一个”然后“上一个”后的表单
after.png

编辑

抱歉,图像早已消失。

I'm working on an ASP.NET web app where I'm using a Wizard to take the client through a large series of steps.

One of the steps includes calculating a bunch of numbers on the fly... the numbers calculate properly but when I click "next" and then go back again... some of the numbers are not retained.

Here is the calculation function

function CalculateFields() {

    txtSellingPrice = document.getElementById('<%=txtSellingPrice.ClientID %>');
    //I get all of the elements like this...
    //...   

    var regexp = /[$,]/g;


    //Empty value checks
    SellingPrice = (SellingPrice == "" ? "$0" : SellingPrice);
    BalanceSheet = (BalanceSheet == "" ? "$0" : BalanceSheet);
    DownPayment = (DownPayment == "" ? "$0" : DownPayment);
    susearn = (susearn == "" ? "$0" : susearn);
    susrev = (susrev == "" ? "$0" : susrev);
    balmult = (balmult == "" ? "$0" : balmult);
    goodmult = (goodmult == "" ? "$0" : goodmult);
    sellmult = (sellmult == "" ? "$0" : sellmult);


    //Replace $ with String.Empty
    SellingPrice = txtSellingPrice.value.replace(regexp, "");
    BalanceSheet = txtBalanceSheet.value.replace(regexp, "");
    DownPayment = txtDownPayment.value.replace(regexp, "");
    susearn = txtSusEarn.value.replace(regexp, "");
    susrev = txtSusRev.value.replace(regexp, "");
    balmult = txtBalanceMult.value.replace(regexp, "");
    goodmult = txtGoodwillMult.value.replace(regexp, "");
    sellmult = txtSellingPriceMult.value.replace(regexp, "");


    //Set the new values
    txtGoodWill.value = "
quot; + (SellingPrice - BalanceSheet);
    txtBalance.value = "
quot; + (SellingPrice - DownPayment);
    txtSellingPriceMult.value = "
quot; + SellingPrice;
    txtGoodwillMult.value = "
quot; + (SellingPrice - BalanceSheet);
    txtBalanceMult.value = "
quot; + BalanceSheet;

    if (chkTakeBack.checked == 1) {
        txtVendorTakeBackPercentage.value = Math.round((SellingPrice - DownPayment) / SellingPrice * 100);
    }
    else {
        txtVendorTakeBackPercentage.value = "0";
    }

    if (!(susearn == "") && !(susearn == "0") && !(susearn == "$0")) {
        txtSusEarnPercentage.value = Math.round(susearn / susrev * 100);
        txtBalanceMultPercentage.value = Math.round(balmult / susearn);
        txtGoodwillMultPercentage.value = Math.round(goodmult / susearn);
        txtSellingPriceMultPercentage.value = Math.round(sellmult / susearn);
    }
    else {
        txtSusEarnPercentage.value = "0";
        txtBalanceMultPercentage.value = "0";
        txtGoodwillMultPercentage.value = "0";
        txtSellingPriceMultPercentage.value = "0";
    }

}

all of these calculate properly and retain their value across postbacks

    txtGoodWill.value = "
quot; + (SellingPrice - BalanceSheet);
    txtBalance.value = "
quot; + (SellingPrice - DownPayment);
    txtSellingPriceMult.value = "
quot; + SellingPrice;
    txtGoodwillMult.value = "
quot; + (SellingPrice - BalanceSheet);
    txtBalanceMult.value = "
quot; + BalanceSheet;

These ones however do not retain their value across postbacks

        if (chkTakeBack.checked == 1) {
            txtVendorTakeBackPercentage.value = Math.round((SellingPrice - DownPayment) / SellingPrice * 100);
        }
        else {
            txtVendorTakeBackPercentage.value = "0";
        }

        if (!(susearn == "") && !(susearn == "0") && !(susearn == "$0")) {
            txtSusEarnPercentage.value = Math.round(susearn / susrev * 100);
            txtBalanceMultPercentage.value = Math.round(balmult / susearn);
            txtGoodwillMultPercentage.value = Math.round(goodmult / susearn);
            txtSellingPriceMultPercentage.value = Math.round(sellmult / susearn);
        }
        else {
            txtSusEarnPercentage.value = "0";
            txtBalanceMultPercentage.value = "0";
            txtGoodwillMultPercentage.value = "0";
            txtSellingPriceMultPercentage.value = "0";
        }

The txtVendorTakeBackPercentage always comes back BLANK
and the other three always come back as 0

I'm firing these functions by using the onkeyup event within the form fields.

    If Not Page.IsPostBack Then
        txtSellingPrice.Attributes.Add("onkeyup", "CalculateFields()")
        txtBalanceSheet.Attributes.Add("onkeyup", "CalculateFields()")
        txtDownPayment.Attributes.Add("onkeyup", "CalculateFields()")
        txtSusRev.Attributes.Add("onkeyup", "CalculateFields()")
        txtSusEarn.Attributes.Add("onkeyup", "CalculateFields()")
    End If

any thoughts/help/direction would be greatly appreciated.

EDIT:
My page load event looks something like this...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load


    'Prepair page for advanced validation'
    Dim cs As ClientScriptManager = Page.ClientScript
    If Not cs.IsOnSubmitStatementRegistered(Me.[GetType](), "PrepareValidationSummary") Then _
        cs.RegisterOnSubmitStatement(Me.[GetType](), "PrepareValidationSummary", "CheckForm()")

    If Not Page.IsPostBack Then

        'Part of the client side math'
        txtSellingPrice.Attributes.Add("onkeyup", "CalculateFields()")
        txtBalanceSheet.Attributes.Add("onkeyup", "CalculateFields()")
        txtDownPayment.Attributes.Add("onkeyup", "CalculateFields()")
        txtSusRev.Attributes.Add("onkeyup", "CalculateFields()")
        txtSusEarn.Attributes.Add("onkeyup", "CalculateFields()")

My actual aspx page has got over 2400 lines, so I'm not going to post the whole thing. Here is a section where the percentage fields are not retaining their values.

  <tr>
                        <td colspan="5" class="AlignRight">
                            Average Annual Sustainable Earnings (ebitda) estimated 
                            <ew:NumericBox ID="txtSusEarnPercentage" runat="server" Enabled="False" MaxLength="3"
                                Width="20px" TruncateLeadingZeros="True" />
                            %
                        </td>
                        <td class="AlignRight">
                            <ew:NumericBox ID="txtSusEarn" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
                                MaxLength="9" Width="92px" />
                        </td>
                        <td>
                            <asp:Image ID="HelpEBITDA0" runat="server" Height="16px" ImageUrl="~/Images/Icons/help.gif" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" class="AlignRight">
                             
                        </td>
                        <td class="AlignRight">
                             
                        </td>
                        <td>
                             
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" class="AlignRight">
                            Balance Sheet / Net Asset Value as earnings multiple  
                            <ew:NumericBox ID="txtBalanceMultPercentage" runat="server" Enabled="False" MaxLength="3"
                                Width="20px" />
                        </td>
                        <td class="AlignRight">
                            <ew:NumericBox ID="txtBalanceMult" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
                                MaxLength="9" Width="92px" Enabled="False" />
                        </td>
                        <td>
                             
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" class="AlignRight">
                            Goodwill Value as earnings multiple  
                            <ew:NumericBox ID="txtGoodwillMultPercentage" runat="server" Enabled="False" MaxLength="3"
                                Width="20px" />
                        </td>
                        <td class="AlignRight">
                            <ew:NumericBox ID="txtGoodwillMult" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
                                MaxLength="9" Width="92px" Enabled="False" />
                        </td>
                        <td>
                             
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" class="AlignRight">
                            Proposed Selling Price as earnings multiple  
                            <ew:NumericBox ID="txtSellingPriceMultPercentage" runat="server" Enabled="False"
                                MaxLength="3" Width="20px" />
                        </td>
                        <td class="AlignRight">
                            <ew:NumericBox ID="txtSellingPriceMult" runat="server" AutoFormatCurrency="True"
                                DecimalPlaces="2" MaxLength="9" Width="92px" Enabled="False" />
                        </td>
                        <td>
                             
                        </td>
                    </tr>

Here is the form when I fill it in
before.png

And here is the form after I hit "next" and then "previous"
after.png

Edit

Images are long gone, sorry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-09-02 07:12:29

asp.net 不维护只读或禁用文本框的值...要解决此问题,请使用:

TextBox1.Attributes.Add("readonly","re​​adonly"); 而不是 TextBox1 .ReadOnly = true;

TextBox1.Attributes.Add("disabled","disabled"); 而不是 TextBox1.Enabled = false;

让我知道如果这解决了您的问题。

asp.net does not maintain the values of readonly or disabled textboxes ... to work around this use :

TextBox1.Attributes.Add("readonly","readonly"); instead of TextBox1.ReadOnly = true;

and

TextBox1.Attributes.Add("disabled","disabled"); instead of TextBox1.Enabled = false;

Let me know if this solves your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文