在javascript中添加表值会在NaN的表格单元格中给出结果

时间:2012-04-27 21:12:01

标签: javascript

我在总表格单元格值中得到NaN的结果。我在每次用户输入数量时添加两个计算单元格。我想弄明白并理解为什么我得到这个结果。这是包含计算功能的代码。

<html>
<head>
<title> Cart </title>
<h1> My cart </h1>
<script type = "text/javascript">

function retr()
{
    var cke = document.cookie;
    var tot1 = document.getElementById("t1").value;
    var tot2 = document.getElementById("t2").value;

    if (cke.length>0) { 
         start = cke.indexOf("Circle=");
         if (start!= -1) {
             start = start + 7
             end = cke.indexOf("$", start);
             if (end == -1) end = cke.length;
             qu1 = cke.substring(start, end);
             document.getElementById("q1").value = qu1;
        }
    }

    if (cke.length>0) {
        start1 = cke.indexOf("$");
        if (start1!= -1) {
            start1 = start1 + 0
            end = cke.indexOf("e", start1);
            if (end == -1) end = cke.length;
            pr1 = cke.substring(start1, end);
            document.getElementById("p1").value = pr1;

        }
    }

    document.getElementById("t1").value = parseFloat(qu1) * parseFloat(pr1);

    if (cke.length>0) {
        start = cke.indexOf("Stickman=");
        if (start!= -1) {
            start = start + 9
            end = cke.indexOf("$", start);
            if (end == -1) end = cke.length;
            qu2 = cke.substring(start, end);
            document.getElementById("q2").value = qu2;
        }
    }

    if (cke.length>0) {
        start = cke.indexOf("$");
        if (start!= -1) {
            start = start + 54
            end = cke.indexOf("e", start);
            if (end == -1) end = cke.length;
            pr2 = cke.substring(start, end);
            document.getElementById("p2").value = pr2;
        }
    }

    alert(qu1);
    alert(pr1);
    alert(qu2);
    alert(pr2);
    alert(t2);
    document.getElementById("t2").value = parseFloat(qu2) * parseFloat(pr2);
    document.getElementById("tot").value = parseFloat(tot1) + parseFloat(tot2);
    document.getElementById("ret").value = cke;
}


</script>
</head>

<body onload = "retr()">
<table border = "1">
    <td>Circle </td>
    <td><input type = "text" size = "8" id = "q1" readonly = "readonly" /></td> 
    <td> <input type = "text" size = "8" id = "p1" readonly = "readonly" /> </td>
    <td> <input type = "text" size = "8" id ="t1">  </td>
    <tr> </tr>
    <td> Stickman </td>
    <td> <input type = "text" size = "8" id = "q2" readonly = "readonly" /></td>
    <td> <input type = "text" size = "8" id = "p2" readonly = "readonly" /> </td>
    <td> <input type = "text" size = "8" id = "t2" >  </td>
    <tr> </tr>
    <td colspan = "3">  TOTAL:</td>
    <td> <input type = "text" size = "8" id = "tot">  </td>
</table> 
<br /> <br />
<input type ="text" id = "ret" readonly = "readonly" />
<br / > <br />
<input type = "button" value = "Checkout">
<br /> <br />
<a href = "store.html" > Continue Shopping </a>


</body>
</html>

2 个答案:

答案 0 :(得分:1)

好的,假设你的cookie看起来像这样:

"Circle=123$456e; Stickman=123$456e"

我修复了你要运行的代码。这是一个有效的例子:

jsfiddle example

您的代码中有几个问题,要点:

  1. 你保留了“$”符号,这不是一个数字因此你得到了 NaN
  2. 你没有设置变量tot1和tot2 填写,或者至少你没有提供默认值。
  3. 在搜索第二个价格时,您没有使用最后一个“$”符号的start1位置作为起点。因此你最终得错了。

答案 1 :(得分:0)

您是否熟悉JavaScript值:Not-a-Number(NaN)?听起来有些用户输入的值正在生成一个基本上是非法数字的结果。