87,907
社区成员
发帖
与我相关
我的任务
分享
<!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=gb2312" />
<title>简易购物车</title>
<script language="javascript">
function ator()
{
var price = parseFloat(document.getElementById("price").value, 10);
var num = parseInt(document.getElementById("num").value, 10);
var sum = Math.round(price*num*100)/100;
document.getElementById("sum").value = sum;
}
</script>
</head>
<style type="text/css">
h1{
text-align:center;
}
td{
height:30px;
}
.title{
background:#66CCFF;
}
</style>
<body>
<h1>简易购物车</h1>
<HR />
<table width="793" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="title">编号</td>
<td class="title">名称</td>
<td class="title">单价(元)</td>
<td class="title">数量</td>
<td class="title">总价</td>
<td class="title"> </td>
</tr>
<tr>
<td>A-01</td>
<td>ThinkPad SL300</td>
<td><input type="text" name="price" id="price" size="10" /></td>
<td><input type="text" name="num" id="num" size="10" /></td>
<td><input type="text" mame="sum" id="sum"size="10" /></td>
<td><input type="button" name="anniu" value="计算价格" onclick="ator()" /></td>
</tr>
</table>
</body>
</html>
<!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>
<title>简易购物车</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#but1").click(function(){
var price = $.trim($("#price").val());//得到价格
var num = $.trim($("#num").val());//得到数量
var reg = /^[0-9]+$/;
if(IsHasCHZN(num,reg)==false)//数量是否为非数字
{
alert("Page Number Invalid");
return;
}
if(num==0||price==0)
{
alert("不能为0");
}
var reg2 = /^(- ¦\+)?\d+(\.\d+)?$/;
if(IsHasCHZN(price,reg2)==false)//价格是否为非小数
{
alert("Invalid price format");
return;
}
$("#sum").val(price*num);//把计算结果存入 记得给分
});
});
/*检查正则 inputData要检测的字段 regular正则表达示*/
function IsHasCHZN(inputData, regular)
{
return regular.test(inputData)
}
</script>
<style type="text/css">
h1
{
text-align: center;
}
td
{
height: 30px;
}
.title
{
background: #66CCFF;
}
</style>
</head>
<body>
<h1>
简易购物车</h1>
<hr />
<table width="793" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="title">
编号
</td>
<td class="title">
名称
</td>
<td class="title">
单价(元)
</td>
<td class="title">
数量
</td>
<td class="title">
总价
</td>
<td class="title">
</td>
</tr>
<tr>
<td>
A-01
</td>
<td>
ThinkPad SL300
</td>
<td>
<input type="text" id="price" size="10" />
</td>
<td>
<input type="text" id="num" size="10" />
</td>
<td>
<input type="text" id="sum" size="10" />
</td>
<td>
<input type="button" name="anniu" id="but1" value="计算价格" />
</td>
</tr>
</table>
</body>
</html>