0

我正在编写一个脚本,该脚本本质上应该是每次用户在选定的文本框中输入值时更新表单的总成本。我的问题是我不确定为什么即使在通话后它也没有更新。我知道我在某个地方犯了一个错误,我只是不确定在哪里。

 $(document).ready(function () {
      var total = document.getElementById(txtTotalCost);
      ComputeCosts();

      total.blur(function () {
        ComputeCosts();
    });

});

function ComputeCosts()
{
    var amount1 = document.getElementById(txtPAmount1);
    var amount2 = document.getElementById(txtPAmount2);
    var amount3 = document.getElementById(txtPAmount3);
    var amount4 = document.getElementById(txtPAmount4);
    var amount5 = document.getElementById(txtPAmount5);
    var totalBox = document.getElementById("txtTotalCost");          

    var totalGift = (txtPAmount1.val() +txtPAmount2.val() + txtPAmount3.val()+txtPAmount4.val()+txtPAmount5.val()).toFixed(2);
    totalBox.val(totalGift);
}

这是它的html方面:

<asp:TextBox runat="server" ID="txtPAmount1" CssClass="narrow" onClick="ComputeCosts();" text="0.00" />
<asp:TextBox runat="server" ID="txtPAmount2" CssClass="narrow" onClick="ComputeCosts();" text="0.00" />  
<asp:TextBox runat="server" ID="txtPAmount3" CssClass="narrow" onClick="ComputeCosts();" text="0.00" />  
<asp:TextBox runat="server" ID="txtPAmount4" CssClass="narrow" onClick="ComputeCosts();" text="0.00" />  
 <asp:TextBox runat="server" ID="txtTotalCost" CssClass="narrow" onClick="ComputeCosts();" text="0.00" />
4

3 回答 3

0

1) 传递客户 ID

<%=ElementName.ClientID%>

document.getElementById("<%=ElementName.ClientID%>")

2) 使用 onClientClick 而不是 onClick

3)value改用val() 或使用

var amount1 = $("#<%=txtPAmount1.ClientID%>");
于 2013-03-06T15:53:46.243 回答
0

服务器控件需要设置为ClientIDMode="Static"或用于<%= %>从客户端访问。

您还想将字符串转换为浮点数以计算总数。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextBoxDemo.aspx.cs" 
    Inherits="WebApplication2012.TextBoxDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="/Scripts/jquery-1.9.1.js"></script>
</head>
<body>
<form id="form1" runat="server">

<asp:TextBox runat="server" ID="txtPAmount1" CssClass="narrow" 
    onClick="ComputeCosts();" Text="0.00" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtPAmount2" CssClass="narrow" 
    onClick="ComputeCosts();" Text="0.00" ClientIDMode="Static"/>
<asp:TextBox runat="server" ID="txtPAmount3" CssClass="narrow" 
    onClick="ComputeCosts();" Text="0.00" ClientIDMode="Static"/>
<asp:TextBox runat="server" ID="txtPAmount4" CssClass="narrow" 
    onClick="ComputeCosts();" Text="0.00" ClientIDMode="Static"/>
<asp:TextBox runat="server" ID="txtPAmount5" CssClass="narrow" 
    onClick="ComputeCosts();" Text="0.00" ClientIDMode="Static"/>
<asp:TextBox runat="server" ID="txtTotalCost" CssClass="narrow" 
    onClick="ComputeCosts();" Text="0.00" ClientIDMode="Static"/>

<script>
    $(document).ready(function () {
        var total = $('#txtTotalCost');
        ComputeCosts();

        total.blur(function () {
            ComputeCosts();
        });
    });

    function ComputeCosts() {
        var amount1 = parseFloat($('#txtPAmount1').val());
        var amount2 = parseFloat($('#txtPAmount2').val());
        var amount3 = parseFloat($('#txtPAmount3').val());
        var amount4 = parseFloat($('#txtPAmount4').val());
        var amount5 = parseFloat($('#txtPAmount5').val());

        var totalGift = (amount1+ amount2 + amount3 + amount4 + amount5);
        $('#txtTotalCost').val(totalGift);
    }
</script>
</form>
</body>
</html>
于 2013-03-06T16:07:35.217 回答
0

删除内联 onclick,并使用以下 jQuery:

$(function () {
    ComputeCosts();

    $(".narrow").on('click', ComputeCosts).addBack().last().on('blur', ComputeCosts);

    /* 
    Above should work, if not seperate the binds
    $(".narrow").on('click', ComputeCosts);
    $("#txtTotalCost").on('blur', ComputeCosts);
    */
});

function ComputeCosts() {
    var amount1 = parseFloat($("#txtPAmount1").val());
    var amount2 = parseFloat($("#txtPAmount2").val());
    var amount3 = parseFloat($("#txtPAmount3").val());
    var amount4 = parseFloat($("#txtPAmount4").val());
    var amount5 = parseFloat($("#txtPAmount5").val());

    var totalGift = (amount1 + amount2 + amount3 + amount4 + amount5).toFixed(2);
    $("#txtTotalCost").val(totalGift);
}
于 2013-03-06T16:09:41.427 回答