简体   繁体   English

我正在尝试使用JavaScript创建测验,但我的按钮无法正常工作

[英]I'm trying to create a quiz with JavaScript and I can't get my button to work

I can't get my head around JavaScript at all, I'm trying to create a quiz but every way I go about it, I can't seem to get the form to submit. 我根本无法理解JavaScript,我正在尝试创建一个测验,但是无论如何,我似乎都无法提交表单。 This is just the section of the code from my HTML website. 这只是我的HTML网站中的代码部分。

<b>Quiz</b>
<br><br>
    <center><p>Q 1) What colour is this laptop?</p>
        <table width="200">
            <tr>
            <td><input type="radio" name="radio" id="a1" value="a1" />
                Orange</td>
            <td><input type="radio" name="radio" id="a2" value="a1" />
                White</td>
            </tr>
            <tr>
            <td><input type="radio" name="radio" id="a3" value="a1" />
                Blue</td>
            <td><input type="radio" name="radio" id="a4" value="a1" />
                Red</td>
            </tr>
        </table>
        <p>
        <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit"
            onClick="fSubmit()" /></center>
        </p>
            <script type="text/javascript">
                function fSubmit(){
                    var correctanswer = document.getElementById("a3")
                        if (correctanswer.checked *** true) {
                            alert("Correct!")
                        }
                        else {
                            alert("Incorrect!")
                        }
                    }   
            </script>
    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <b>Quiz</b>
    <br><br>
    <form>
    <center>
        <p>Q 1) What colour is this laptop?</p>
        <table width="200">
            <tr>
                <td>
                    <input type="radio" name="radio" id="a1" value="a1" />
                    Orange
                </td>
                <td>
                    <input type="radio" name="radio" id="a2" value="a1" />
                    White
                </td>
            </tr>
            <tr>
                <td>
                    <input type="radio" name="radio" id="a3" value="a1" />
                    Blue
                </td>
                <td>
                    <input type="radio" name="radio" id="a4" value="a1" />
                    Red
                </td>
            </tr>
        </table>
        <p>
            <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit"
                   onclick="fSubmit()" />
    </center>
    </form>
    </p>
    <script type="text/javascript">
        function fSubmit() {
                    var correctanswer = document.getElementById("a3")
                        if (correctanswer.checked === true) {
                            alert("Correct!")
                        }
                        else {
                            alert("Incorrect!")
                        }
                    }
    </script>
</body>
</html>

Add the form tag. 添加表单标签。 It will work in some browsers without form tag. 它将在某些没有表单标签的浏览器中运行。 It is a good practice to place a form tag. 放置表单标签是一个好习惯。 Also in if, replace *** with === 同样在if中,将***替换为===

If there is a JavaScript error (such as the *** instead of === in your code example), the click event will not fire. 如果出现JavaScript错误(例如代码示例中的***而不是===),则不会触发click事件。 You can open up the JavaScript debugger (sometimes called console) in your browser to see any errors. 您可以在浏览器中打开JavaScript调试器(有时称为控制台)以查看任何错误。

Also, it seems as though you do not want to "submit" your button click to the server, so the input type should be button (not submit) or else use the on submit event on the form instead of the onclick event on the button. 另外,似乎您不想将按钮单击“提交”到服务器,因此输入类型应为按钮(而不是提交),否则请使用表单上的on Submit事件而不是按钮上的onclick事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我刚开始学习 javascript,我正在尝试制作一个计算器网站,但无法让它工作 - i just started learning javascript and i'm trying to make a calculator website but can't get it to work 我无法使用jQuery,也不确定自己在做什么错 - I can't get my jQuery to work and I'm not sure what I'm doing wrong 我正在尝试使用jquery创建一个包含列的div,但是我无法正确格式化数组 - I'm trying to use jquery to create a div containing columns but I can't get my array to format correctly 如何获得与我的其余javascript一起使用的按钮? - How can I get my button to work with the rest of my javascript? 我正在尝试将 Javascript 计算器作为 freecodecamp 任务的一部分,但无法使其与 ID 和数据运算符一起使用 - I'm trying to make a Javascript calculator as part of a freecodecamp task but can't get it to work with IDs and data-operators 我正在尝试动态创建javascript Dom单选按钮和属性 - I'm trying to create a javascript Dom radio button and attribute dynamically 我正在尝试制作一个程序来锁定一个网站,但我无法让它工作 - I'm trying to make a program to lock a website, but I can't get it to work 我怎样才能让 Javascript 显示我的 JavaScript 代码中的错误,为什么我的提交按钮不起作用? - How can I get Javascript to display the errors that are in my JavaScript code and why doesn't my submit button work? React Search - 我正在尝试创建一个过滤器,但它不起作用 - React Search - I'm trying to create a filter and it doesn't work 我正在尝试从堆栈创建队列,但出现错误“找不到所需的变量” - I m trying to create a queue from stacks but I get the error “Can't find the variable require”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM