我在页面上有 3 个文本框:数量、价格和总计。当用户输入数量和价格时,将使用 JavaScript 直接计算总数。 下面是代码:
function quantity(textvalue) {
var qty = textvalue.value;
var price = document.getElementById('<%=Price.ClientID %>').value;
var Total = Math.round(qty * price * 100) / 100;
document.getElementById('<%=Total.ClientID %>').value = Total;
}
function Price(textvalue) {
var price = textvalue.value;
var qty = document.getElementById('<%=Quantity.ClientID %>').value;
var Total = Math.round(qty * price * 100) / 100;
document.getElementById('<%=Total.ClientID %>').value = Total;
}
<asp:TextBox ID="Quantity" runat="server" onchange="javascript: quantity(this);" CausesValidation="True"></asp:TextBox>
<asp:TextBox ID="Price" runat="server" onchange="javascript: Price(this);" ></asp:TextBox>
<asp:TextBox ID="txtLineItemTotal" runat="server" Width="120px" MaxLength="14" ReadOnly="true"
BackColor="Silver" BorderWidth="2px" style="font-weight: 700">0.00</asp:TextBox>
我还需要验证总计文本框中计算的值,它不应超过 100000000000.00。
我正在使用以下函数来实现此目的
function TotalChanged() {
var lineitemtotal = document.getElementById('<%=Total.ClientID %>').value;
alert("entered function");
if (lineitemtotal >= 100000000000.00) {
alert("Total cannot exceed 100000000000.00,please re-enter");
}
else
return false;
}
在页面加载时
Total.Attributes.Add("onchange", "{return TotalChanged()};");
但它没有进入该功能,请让我知道我错过了什么...或者如果我使用了错误的事件...在过去的 1 天中尝试过......:(
最佳答案
您不应该需要 javascript:
在 onchange 中。
<asp:TextBox ID="Quantity" runat="server" onchange="javascript: quantity(this);" CausesValidation="True"></asp:TextBox>
将渲染到
<input type="text" id="Quality" name="Quality" onchange="javascript: quantity(this);"/>
西蒙
关于javascript - 使用 javascript 验证号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6975021/