<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script language="javascript">
function calc()
{
//1 读取三个控件的值
var d1=document.getElementById("txtData1").value;
var d2=document.getElementById("txtData2").value;
var kj=document.getElementById("lstOperator");
var op=kj.options[kj.selectedIndex].value;//.text;
// 2 判断内容的规范性
/*if(!(d1[0]=='-'||(d1[i]>='0'&&d1[i]<='9')))
{
alert("只允许数字运算");
return;
}
for(i=1;i<d1.length;i++)
{
if(!(d1[i]=='.'||(d1[i]>='0'&&d1[i]<='9')))
{
alert("只允许数字运算");
return;
}
}*/
//alert(d1+op+d2);
//3 运算出结果
alert(d1+op+d2+"="+eval(d1+op+d2));
}
</script>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="txtData1"></label>
迷你计算器</p>
<p>
<input type="text" name="txtData1" id="txtData1" />
<label for="lstOperator"></label>
<select name="lstOperator" id="lstOperator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<label for="txtData2"></label>
<input type="text" name="txtData2" id="txtData2" />
<input type="button" name="btView" id="btView" value="查看结果" onclick="calc()"/>
</p>
</form>
</body>
</html>
Powered by ddoss.cn 12.0
©2015 - 2024 ddoss
渝公网安备50011302222260号 渝ICP备2024035333号 【实验平台安全承诺书】 小绿叶技术社区,优化网络中,点击查看配置信息
您的IP:192.168.122.82,2024-10-10 19:52:58,Processed in 0.04053 second(s).