asp.net - Javascript 计算和回传

标签 asp.net javascript math calculated-field

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

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

这里是计算函数

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";
    }

}

所有这些都正确计算并在回传中保留它们的值(value)

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

然而,这些在回传中不保留它们的值(value)

        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

这是我点击“下一个”然后“上一个”后的表格
<罢工>后.png

编辑

图片早就不见了,抱歉。

最佳答案

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

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

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

如果这能解决您的问题,请告诉我。

关于asp.net - Javascript 计算和回传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2494799/

相关文章:

javascript - 如何将 JSON 数据传递到 extJS 存储模型中。

java - Math.round在android中给出一个十进制数

c# - 如何在 DotNetNuke 中以编程方式创建文件夹并为其分配权限?

html - 使 ajax TabPanel 的背景半透明

c# - 如何在鼠标悬停在 C# Web 应用程序上的 Ms 图表中显示数据

javascript - Angularjs 表单提交和导航到其他页面

javascript - 如何使用两个提交函数来更新保存表单 Angular js

c# - 我可以使用什么数学公式来获取给定日期的日期(例如星期五)?

python - 许多数组的插值的矢量化总和

php - 将网站服务嵌入另一个网站的最佳方式