简易购物车 数量乘以价格 为什么没有结果?

java120936434 2011-11-29 11:48:15
<!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.all.price.value);
var num=parseInt(document.all.num.value);
var sum=price*num;
document.all.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>

<td><input type="text" mame="sum" id="sum"size="10" /></td> 运行中应该这个是有值的 为什么没有反应啊?
...全文
205 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sparta 2011-11-30
  • 打赏
  • 举报
回复
要兼容啊。document.getElementById().value
稍微好点,建议用jquery 直接 $("#id").val(赋值了)
hexinping123 2011-11-29
  • 打赏
  • 举报
回复
document.all只有在ie中才支持,在DOM下就不行了,所以为了保证兼容性,都用document.getElementById("")来获取标签元素的 改成下面的就好了

<script language="javascript">
function ator()
{
var price=parseFloat(document.getElementById("price").value);
var num=parseInt(document.getElementById("num").value);
var sum=price*num;
document.getElementById("sum").value=sum;
}
</script>
oggmm 2011-11-29
  • 打赏
  • 举报
回复
其次做一些输入的有效性检查
wishifef 2011-11-29
  • 打赏
  • 举报
回复
<td><input type="text" mame="sum" id="sum"size="10" /></td>

---->name
oggmm 2011-11-29
  • 打赏
  • 举报
回复
建议用document.getElementById("price").value
hookee 2011-11-29
  • 打赏
  • 举报
回复

<!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>
默默不得鱼 2011-11-29
  • 打赏
  • 举报
回复
NaN了吧
另外document.all. 只有IE认识你这种写法 注意规范
汉尼拔 2011-11-29
  • 打赏
  • 举报
回复
犯了这种拼写错误是非常头疼的事
tangxingangtom 2011-11-29
  • 打赏
  • 举报
回复

<!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>

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧