<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Js计算题</title>
</head>
<body>
<form id="form1" runat="server">
<div>
A:<asp:TextBox ID="textBoxA" runat="server" Columns="5" />
B:<asp:TextBox ID="textBoxB" runat="server" Columns="5" />
C:<asp:TextBox ID="textBoxC" runat="server" Columns="5" />
Result:<asp:TextBox ID="textBoxD" runat="server" Columns="5" />
</div>
</form>
</body>
</html>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
string calcExpression = "calc(['" + textBoxA.ClientID + "','" + textBoxB.ClientID + "','" + textBoxC.ClientID + "','" + textBoxD.ClientID + "'])";
textBoxA.Attributes.Add("onkeyup", calcExpression);
textBoxB.Attributes.Add("onkeyup", calcExpression);
textBoxC.Attributes.Add("onkeyup", calcExpression);
base.OnLoad(e);
}
</script>
<script language="javascript">
String.prototype.trim = function(){ var r = /^\s+|\s+$/g;return this.replace(r,'');};
String.prototype.isNumber = function(){var reg = /^[\d]*[.]?\d*$/; var t = this; return reg.test(t);};
function $id(id){return document.getElementById(id);}
function calc(otxtArray){
if(otxtArray && otxtArray.length && otxtArray.length > 2){
var result = 1;
for(var i=0;i<otxtArray.length-1;i++){
var otxt = $id(otxtArray[i]);
var iValue =otxt.value.trim();
//如果是空值,则退出函数不进行计算
if(!iValue){
return;
}
if(!iValue.isNumber()){
//此处是否提示根据需要定
alert('请输入数字');
return;
}
iValue = parseFloat(iValue);
//如果为0不进行计算
if(iValue <= 0){
//此处是否提示根据需要定
alert('请输入大于0的数字');
return;
}
result = result * iValue;
}
var oresult = $id(otxtArray[otxtArray.length-1]);
oresult.value = result;
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Js计算题</title>
</head>
<body>
<form id="form1" runat="server">
<div>
A:<asp:TextBox ID="textBoxA" runat="server" Columns="5" />
B:<asp:TextBox ID="textBoxB" runat="server" Columns="5" />
C:<asp:TextBox ID="textBoxC" runat="server" Columns="5" />
Result:<asp:TextBox ID="textBoxD" runat="server" Columns="5" />
</div>
</form>
</body>
</html>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
string calcExpression = "calc(['" + textBoxA.ClientID + "','" + textBoxB.ClientID + "','" + textBoxC.ClientID + "','" + textBoxD.ClientID + "'])";
textBoxA.Attributes.Add("onkeyup", calcExpression);
textBoxB.Attributes.Add("onkeyup", calcExpression);
textBoxC.Attributes.Add("onkeyup", calcExpression);
base.OnLoad(e);
}
</script>
<script language="javascript">
String.prototype.trim = function(){ var r = /^\s+|\s+$/g;return this.replace(r,'');};
String.prototype.isNumber = function(){var reg = /^[\d]*[.]?\d*$/; var t = this; return reg.test(t);};
function $id(id){return document.getElementById(id);}
function calc(otxtArray){
if(otxtArray && otxtArray.length && otxtArray.length > 2){
var result = 1;
for(var i=0;i<otxtArray.length-1;i++){
var otxt = $id(otxtArray[i]);
var iValue =otxt.value.trim();
//如果是空值,则退出函数不进行计算
if(!iValue){
return;
}
if(!iValue.isNumber()){
//此处是否提示根据需要定
alert('请输入数字');
return;
}
iValue = parseFloat(iValue);
//如果为0不进行计算
if(iValue <= 0){
//此处是否提示根据需要定
alert('请输入大于0的数字');
return;
}
result = result * iValue;
}
var oresult = $id(otxtArray[otxtArray.length-1]);
oresult.value = result;
}
}
</script>