为了账号安全,请及时绑定邮箱和手机立即绑定

关于简易计算器NaN的问题

关于简易计算器NaN的问题

karlmarx 2016-05-24 02:23:43
<!DOCTYPE html> <html> <head>     <title> 事件</title> </head> <body>     <input type='text' id='txt1' />     <select id='select'>         <option value='+'>+</option>         <option value="-">-</option>         <option value="*">*</option>         <option value="/">/</option>     </select>     <input type='text' id='txt2' />     <input type='button' value=' = ' onclick="count()" />     <!--通过 = 按钮来调用创建的函数,得到结果-->     <input type='text' id='fruit' />     <script type="text/javascript">         var a = parseInt(document.getElementById('txt1')).value;         var b = parseInt(document.getElementById('txt2')).value;         var c = document.getElementById('select').value;         var d;         function count() {             if (c == '+') {                 d = a + b;                 document.getElementById('fruit').value = d;             } else if (c == '-') {                 d = a - b;                 document.getElementById('fruit').value = d;             } else if (c == '*') {                 d = a * b;                 document.getElementById('fruit').value = d;             } else if (c == '/') {                 d = a / b;                 document.getElementById('fruit').value = d;             } else if (isNaN(a) || isNaN(b)) {                 alert('请输入数字!');             } else if (a.length == 0) {                 alert('请输入数字!');             } else if (b.length == 0) {                 alert('请输入数字!');             } else if (b == 0) {                 alert('除数不能为0!');             }     </script> </body> </html>为什么执行一直有问题,输出一直是NaN?   
查看完整描述

1 回答

已采纳
?
玩意11

TA贡献14条经验 获得超10个赞


<!DOCTYPE html>
<html>

<head>
    <title> 事件</title>

</head>

<body>
    <input type='text' id='txt1' />
    <select id='select'>
        <option value='+'>+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
    </select>
    <input type='text' id='txt2' />
    <input type='button' value=' = ' onclick="count()" />
    <!--通过 = 按钮来调用创建的函数,得到结果-->
    <input type='text' id='fruit' />
    <script type="text/javascript">

    function count() {
    var a = parseInt(document.getElementById('txt1').value);

    var b = parseInt(document.getElementById('txt2').value);

    var c = document.getElementById('select').value;

    var d;
        if (c == '+') {
            d = a + b;
            document.getElementById('fruit').value = d;
        } else if (c == '-') {
            d = a - b;
            document.getElementById('fruit').value = d;
        } else if (c == '*') {
            d = a * b;
            document.getElementById('fruit').value = d;
        } else if (c == '/') {
            d = a / b;
            document.getElementById('fruit').value = d;
        } else if (isNaN(a) || isNaN(b)) {
            alert('请输入数字!');
        } else if (a.length == 0) {
            alert('请输入数字!');
        } else if (b.length == 0) {
            alert('请输入数字!');
        } else if (b == 0) {
            alert('除数不能为0!');


        }
    }
    </script>
</body>

</html>


  1. count()函数你最后少了个}括号;

  2. parseInt(document.getElementById('txt1').value)。parseInt的应该是到value的后面,你也写错位置了

查看完整回答
1 反对 回复 2016-05-24
  • 1 回答
  • 0 关注
  • 1734 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信