我正在开发一个脚本,每当用户在所选文本框中输入值时,该脚本基本上应该更新表单的总成本。我的问题是我不确定为什么即使在通话后它也没有更新。我知道我在某个地方犯了一个错误我还不知道在哪里。
$(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" />
答案 0 :(得分:0)
1)传递客户ID
<%=ElementName.ClientID%>
document.getElementById("<%=ElementName.ClientID%>")
2)使用onClientClick而不是onClick
3)使用value
代替val()
或使用
var amount1 = $("#<%=txtPAmount1.ClientID%>");
答案 1 :(得分:0)
需要将服务器控件设置为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>
答案 2 :(得分: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);
}