首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >isNaN JavaScript函数

isNaN JavaScript函数
EN

Stack Overflow用户
提问于 2014-11-20 13:43:31
回答 2查看 251关注 0票数 0

我第一次学习如何使用isNaN,想要一些帮助来发现我的错误。我为变量a设置了isNaN,并设置了一个警告框,但是当我输入一个单词或不输入一个数字时,警告就不会出现。

代码语言:javascript
复制
<!DOCTYPE html>
<html>

    <head>
        <title>Project</title>
        <style type=text/css>
            .inbox {
                width=30px;
                text-align: right;
                border: 2px solid black;
            }
            .align {
                text-align: right
            }
        </style>
        <script type="text/javascript">
            function compute() {
                var a = form1.inputA.value;
                a = parseFloat(a);
                var b = form1.inputB.value;
                b = parseFloat(b);
                var c = form1.inputC.value;
                c = parseFloat(c);
                var d = a + b + c;
                form1.quantity.value = d.toFixed(2);
                var e = a * 5.49;
                form1.sumA.value = e.toFixed(2);
                var f = b * 7.49;
                form1.sumB.value = f.toFixed(2);
                var g = c * 6.49;
                form1.sumC.value = g.toFixed(2);
                var h = e + f + g;
                form1.total.value = h.toFixed(2);
                var i = h * .06;
                form1.tax.value = i.toFixed(2);
                var j = i + h;
                form1.sub.value = j.toFixed(2);
            }
            if (isNaN(a)) {
                alert('s');
            }

            function pageInit() {
                form1.inputA.focus();
            }
        </script>
    </head>

    <body onload="pageInit();">
        <form id="form1">
            <table border="2">
                <tr>
                    <th colspan="4">Sample Order Form</th>
                </tr>
                <tr>
                    <th>Quantity</th>
                    <th>item</th>
                    <th>Unit Price</th>
                    <th>Totals</th>
                </tr>
                <tr>
                    <th>
                        <input tabindex="1" class="inbox" type="text" id="inputA" />
                    </th>
                    <th>Apples</th>
                    <td>$5.49</td>
                    <th>
                        <input class="inbox" type="text" id="sumA" readonly="readonly" />
                    </th>
                </tr>
                <tr>
                    <th>
                        <input tabindex="2" class="inbox" type="text" id="inputB" />
                    </th>
                    <th>Pears</th>
                    <td>$7.49</td>
                    <th>
                        <input class="inbox" type="text" id="sumB" readonly="readonly" />
                    </th>
                </tr>
                <tr>
                    <th>
                        <input tabindex="3" class="inbox" type="text" id="inputC" />
                    </th>
                    <th>Grapes</th>
                    <td>$6.49</td>
                    <th>
                        <input class="inbox" type="text" id="sumC" readonly="readonly" />
                    </th>
                </tr>
                <tr>
                    <th>
                        <input class="inbox" type="text" id="quantity" readonly="readonly" />
                    </th>
                    <th class="align" colspan="2">Subtotal</th>
                    <th>
                        <input class="inbox" type="text" id="total" readonly="readonly" />
                    </th>
                </tr>
                <tr>
                    <th class="align" colspan="3">Tax@6%</th>
                    <th>
                        <input class="inbox" type="text" id="tax" readonly="readonly" />
                </tr>
                <tr>
                    <th>
                        <input tabindex="4" type="button" value="Compute" onclick="compute();" />
                    </th>
                    <th class="align" colspan="2">Total</th>
                    <th>
                        <input class="inbox" type="text" id="sub" readonly="readonly" />
                    </th>
                </tr>
            </table>
        </form>
    </body>

</html>
EN

回答 2

Stack Overflow用户

发布于 2014-11-20 13:57:37

你会得到错误Uncaught ReferenceError: a is not defined

当脚本文件加载到页面上并且a不在全局范围内时,将执行下面的块。它在function compute中声明

代码语言:javascript
复制
        if (isNaN(a)) {
            alert('s');
        }

对于您的测试,您可以将if块移到var a = form1.inputA.value;行之后的function compute中,并可以使用它。

如果a不是一个数字,那么当您点击计算按钮时,将显示alert('s')

下面的

是基于跨浏览器的标准推荐方法:

代码语言:javascript
复制
//Add below function which can be useful to get DOM element based on element id.
function $(id) {
  return document.getElementById(id)
}

function compute() {
  var a = $("inputA").value,
    b = $("inputB").value,
    c = $("inputC").value;

  //below is your test alert checking
  if (isNaN(a)) {
    alert('s');
  }

  a = parseFloat(a, 10);
  b = parseFloat(b, 10);
  c = parseFloat(c, 10);

  var d = a + b + c,
    e = a * 5.49,
    f = b * 7.49,
    g = c * 6.49,
    h = e + f + g,
    i = h * .06,
    j = i + h;

  $("quantity").value = d.toFixed(2);
  $("sumA").value = e.toFixed(2);
  $("sumB").value = f.toFixed(2);
  $("sumC").value = g.toFixed(2);
  $("total").value = h.toFixed(2);
  $("tax").value = i.toFixed(2);
  $("sub").value = j.toFixed(2);

}
票数 1
EN

Stack Overflow用户

发布于 2014-11-20 14:09:49

代码语言:javascript
复制
if (isNaN(a)) {
        alert('Please set a number');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27032552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档