猿问

关于简易计算器NaN的问题

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

karlmarx
浏览 1865回答 1
1回答

玩意11

<!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>count()函数你最后少了个}括号;parseInt(document.getElementById('txt1').value)。parseInt的应该是到value的后面,你也写错位置了
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答