<script language="javascript">
//四舍五入
//作者:鲁元忠
Number.prototype.toFixed=function(len)
{
var add = 0;
var s,temp;
var l = this + "";
var start = l.indexOf(".");
if(l.substr(start+len+1,1)>=5)add=1;
var temp = Math.pow(10,len);
s = Math.floor(this * temp) + add;
return s/temp;
}
//实现计算金额
//作者:鲁元忠
function count(obj)
{
var regInt = /^d+$/;
var regMondy = /^(0|[1-9]d*)$|^(0|[1-9]d*).(d+)$/;
// 找出TextBox
var objParent = obj.parentElement.parentElement;
var textBoxs = objParent.getElementsByTagName("input");
var TxtGoodsNum = null;
var TxtGoodsSinglePrice = null;
var TxtGoodsSumPrice = null;
for( var i=0;i < textBoxs.length;i ++)
{
if(textBoxs[i].id.indexOf("txtGoodsNum") != -1)
{
TxtGoodsNum = textBoxs[i];
}
else if(textBoxs[i].id.indexOf("txtGoodsSinglePrice") != -1)
{
TxtGoodsSinglePrice = textBoxs[i];
}
else if(textBoxs[i].id.indexOf("txtGoodsSumPrice") != -1)
{
TxtGoodsSumPrice = textBoxs[i];
}
}
if(!regInt.test(TxtGoodsNum.value))
{
alert("数量只能是数字,请您重新输入数量。");
return;
}
if(!regMondy.test(TxtGoodsSinglePrice.value))
{
alert("单价只能是数字,请您重新输入单价。");
return;
}
var GoodsNum = parseInt(TxtGoodsNum.value); //数量
var SinglePrice = parseFloat(TxtGoodsSinglePrice.value); //单价
var GoodsSumPrice = GoodsNum * SinglePrice;
TxtGoodsSumPrice.value = GoodsSumPrice.toFixed(3).toString();
}
</script>
调用:
<asp:TextBox id=txtGoodsNum onblur=count(this) runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Goods_Num") %>' Width="60px">
//四舍五入
//作者:鲁元忠
Number.prototype.toFixed=function(len)
{
var add = 0;
var s,temp;
var l = this + "";
var start = l.indexOf(".");
if(l.substr(start+len+1,1)>=5)add=1;
var temp = Math.pow(10,len);
s = Math.floor(this * temp) + add;
return s/temp;
}
//实现计算金额
//作者:鲁元忠
function count(obj)
{
var regInt = /^d+$/;
var regMondy = /^(0|[1-9]d*)$|^(0|[1-9]d*).(d+)$/;
// 找出TextBox
var objParent = obj.parentElement.parentElement;
var textBoxs = objParent.getElementsByTagName("input");
var TxtGoodsNum = null;
var TxtGoodsSinglePrice = null;
var TxtGoodsSumPrice = null;
for( var i=0;i < textBoxs.length;i ++)
{
if(textBoxs[i].id.indexOf("txtGoodsNum") != -1)
{
TxtGoodsNum = textBoxs[i];
}
else if(textBoxs[i].id.indexOf("txtGoodsSinglePrice") != -1)
{
TxtGoodsSinglePrice = textBoxs[i];
}
else if(textBoxs[i].id.indexOf("txtGoodsSumPrice") != -1)
{
TxtGoodsSumPrice = textBoxs[i];
}
}
if(!regInt.test(TxtGoodsNum.value))
{
alert("数量只能是数字,请您重新输入数量。");
return;
}
if(!regMondy.test(TxtGoodsSinglePrice.value))
{
alert("单价只能是数字,请您重新输入单价。");
return;
}
var GoodsNum = parseInt(TxtGoodsNum.value); //数量
var SinglePrice = parseFloat(TxtGoodsSinglePrice.value); //单价
var GoodsSumPrice = GoodsNum * SinglePrice;
TxtGoodsSumPrice.value = GoodsSumPrice.toFixed(3).toString();
}
</script>
调用:
<asp:TextBox id=txtGoodsNum onblur=count(this) runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Goods_Num") %>' Width="60px">