<!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">&nbsp;</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> 运行中应该这个是有值的 为什么没有反应啊?

解决方案 »

  1.   

    NaN了吧
    另外document.all. 只有IE认识你这种写法 注意规范
      

  2.   


    <!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">&nbsp;</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>
      

  3.   

    建议用document.getElementById("price").value
      

  4.   

    <td><input type="text" mame="sum" id="sum"size="10" /></td>---->name
      

  5.   

    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>
      

  6.   


    <!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">
                    &nbsp;
                </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>
      

  7.   

    要兼容啊。document.getElementById().value
     稍微好点,建议用jquery 直接 $("#id").val(赋值了)