繁体   English   中英

使用Javascript从文本框实时计算总计

[英]Calculating Total from Textbox live using Javascript

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

 $(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" />

1)传递客户ID

<%=ElementName.ClientID%>

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

2)使用onClientClick代替onClick

3)使用value代替val()或使用

var amount1 = $("#<%=txtPAmount1.ClientID%>");

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

您还希望将字符串转换为float以计算总数。

<%@ 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>

删除内联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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM