<input name="w" type="text" /> *<input name="q" type="text" onblur="document.all.e.value=document.all.w.value*document.all.q.value;" />=
<input name="e" type="text" />
<input name="e" type="text" />
用document.getElementById(id).value来获取值
不难,但是有些烦琐罢了.
<html>
<head>
<script type="text/javascript">
var caculateParam = {
first:"undefined",
second:"undefined",
result:"undefined",
getfirst:function()
{
var val = eval( "this.result/this.second" );
if ( isNaN(val) )
{
return "undefined";
}
return eval( "this.result/this.second" );
},
getsecond:function()
{
var val = eval( "this.result/this.first" );
if ( isNaN(val) )
{
return "undefined";
}
return eval( "this.result/this.first" );
},
getresult:function()
{
var val = eval( "this.first*this.second" );
if ( isNaN(val) )
{
return "undefined";
}
return eval( "this.first*this.second" );
}
};
function get(aParam)
{
return caculateParam[aParam.id];
}
function set(aName,aValue)
{
if ( !isNaN(aValue) )
{
document.getElementById(aName).value = aValue;
}
}
function setParam(aParam)
{
if ( !isNaN(aParam.value) )
{
caculateParam[aParam.id] = aParam.value;
}
else
{
set(aParam.id,"");
}
}
function valueChange(aParam)
{
setParam(aParam);
var breakFlag = false;
var val;
for( p in caculateParam )
{
if ( typeof(caculateParam[p]) == "function" )
{
continue;
}
if ( breakFlag ) return;
try
{
val = caculateParam["get"+p]();
}
catch (e)
{
}
if ( !isNaN(val) )
{
breakFlag = true;
caculateParam[p] = val;
set( p,val );
}
}
}
function clearAll()
{
for( p in caculateParam )
{
if ( typeof(caculateParam[p]) == "function" )
{
continue;
}
set(p,"");
caculateParam[p] = "undefined";
}
}
</script>
</head>
<body>
<input id="first" type="text" value="" onchange="valueChange(this);"/> 1 <br /> <br />
<input id="second" type="text" value="" onchange="valueChange(this);"/> 2 <br /> <br />
<input id="result" type="text" value="" onchange="valueChange(this);"/> 3 <br /> <br />
<input id="clear" type="button" onclick="clearAll();" style="width:60px" value="清空"/>
</body>
</html>