Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中比较整数时遇到问题_Javascript - Fatal编程技术网

在javascript中比较整数时遇到问题

在javascript中比较整数时遇到问题,javascript,Javascript,我在使用比较整数的函数时遇到问题 我分配了输入并使用了parseInt方法,但该方法没有像我预期的那样工作。我考虑过不同的解决方案,但运气不佳 这是我遇到问题的代码部分: <form> <label>QUESTION 4 <br /></label> <label for="q4">How long is the Pieterad, the most

我在使用比较整数的函数时遇到问题

我分配了输入并使用了parseInt方法,但该方法没有像我预期的那样工作。我考虑过不同的解决方案,但运气不佳

这是我遇到问题的代码部分:

            <form>
                <label>QUESTION 4 <br /></label>
                <label for="q4">How long is the Pieterad, the most famous walking trail in Ashington?(In Kilometers)</label>
                <input type="text" id="q4" placeholder="Enter your answer here." />
                <button onclick="q4function()" type="button">Submit</button>
                <p id="q4Answer"></p>
            </form>

        function q4Funtion() {
            var a = parseInt(document.getElementById("q4").value, 10);

            if (a == 498)
                document.getElementById("q4Answer").innerHTML = "Great Success!";
            else if (a != 498)
                document.getElementById("q4Answer").innerHTML = "The answer was 498.";    
        }

问题4
阿辛顿最著名的步行小道Pieterad有多长?(以公里为单位) 提交

函数Q4Function(){ var a=parseInt(document.getElementById(“q4”).value,10); 如果(a==498) document.getElementById(“q4Answer”).innerHTML=“非常成功!”; 否则如果(a!=498) document.getElementById(“q4Answer”).innerHTML=“答案是498。”; }
我没有从这个函数中得到任何结果。我不确定哪里做错了。

如果你正确拼写“Function:”),也可以查看事件处理程序,而不是onclick方法

function q4Function() {
            var a = parseInt(document.getElementById("q4").value, 10);

            if (a == 498)
                document.getElementById("q4Answer").innerHTML = "Great Success!";
            else if (a != 498)
                document.getElementById("q4Answer").innerHTML = "The answer was 498.";    
        }

您的表单可能已提交,这意味着您的JavaScript代码未运行。添加此选项可防止:

document.querySelector("form").addEventListener("submit", e => e.preventDefault());
还要确保您正确拼写
函数

function q4Function() {...}

“不行”是什么意思?发生了什么,您期望发生什么?检查您的浏览器控制台。您的表单可能正在提交中,因此可以添加
event.preventDefault()
进入你的函数你是如何单独测试这是唯一没有结果的函数的?当您的意思是没有结果时,您的意思是它给出了一个“null”响应还是返回空?如果是一回事,为什么要再次检查else分支以了解您期望的结果?如果你已经到了,就不会是498了:)我想你只是打错了-你的函数名是
q4function
,但你正在调用
q4function