Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.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小测验,有4个问题。单选按钮&x2B;继续按钮_Javascript_Html - Fatal编程技术网

Javascript小测验,有4个问题。单选按钮&x2B;继续按钮

Javascript小测验,有4个问题。单选按钮&x2B;继续按钮,javascript,html,Javascript,Html,我试着用4个问题(多于一个)来做一个小测验。每个问题有4个单选按钮和一个继续按钮(input type=“submit”)。我希望当你们点击“继续”按钮时,你们会被重定向到某个页面(success.html),但只有当所有问题都得到正确回答时。当您在问题中出错并单击“继续”按钮时,它将重新加载测验页面 我尝试过这样做: 1.奥塔兹卡:奥塔佐卡 对 不 斯普拉夫纳 阿斯达 var continue_button=document.getElementById('continue'); 功能切换

我试着用4个问题(多于一个)来做一个小测验。每个问题有4个单选按钮和一个继续按钮(
input type=“submit”
)。我希望当你们点击“继续”按钮时,你们会被重定向到某个页面(success.html),但只有当所有问题都得到正确回答时。当您在问题中出错并单击“继续”按钮时,它将重新加载测验页面

我尝试过这样做:


1.奥塔兹卡:奥塔佐卡
对
不
斯普拉夫纳
阿斯达

var continue_button=document.getElementById('continue'); 功能切换(开关元件){ if(switchElement.value='y') continue_button.setAttribute(“onClick”,“window.location.href=”http://www.google.sk'"); 其他的 continue_button.setAttribute(“onClick”,“window.location.href=”http://www.facebook.com'"); }
但它只适用于一个问题

我很高兴能得到一些帮助。 谢谢


谢谢你的回答。但当我选择radiobutton并单击continue时,它并没有将我重定向到任何页面。所以我试试这个

<label><input type="radio" name="otazka1" value="n" /> false</label>
  <label><input type="radio" name="otazka1" value="n" /> false</label>
  <label><input type="radio" name="otazka1" value="y" /> true</label>
  <label><input type="radio" name="otazka1" value="n" /> false</label>
  <input type="submit" id="continue" value="Continue" />
<script>
    var continue_button = document.getElementById('continue');
    continue_button.onclick = function(event) {
        var everything_OK = true;
        var inputs = document.getElementsByTagName("input");
        for (var i=0; i<inputs.length; i++) {
            if (inputs[i].type != "radio")
                continue;
            var should_be = inputs[i].value == "y";
            if (inputs[i].checked != should_be) {
                everything_OK = false;
                break;
            }
        }
        if (everything_OK) {
            // this is not a good success-page
            continue_button.setAttribute("onClick", "window.location.href='succes.html'");
        } else {
            continue_button.setAttribute("onClick", "window.location.href='pop.html'");
        }
    };
</script>
false
假的
真的
假的
var continue_button=document.getElementById('continue');
continue\u button.onclick=函数(事件){
var everything_OK=true;
var inputs=document.getElementsByTagName(“输入”);

对于(var i=0;i您需要循环检查所有问题,并查看在按下“继续”按钮时是否正确检查了这些问题,而不是在仅选择一个答案时更改表单的完整验证状态。请尝试以下操作:

<label><input type="radio" name="otazka1" value="n" /> Yes</label>
<label><input type="radio" name="otazka1" value="n" /> No</label>
<label><input type="radio" name="otazka1" value="y" /> spravna</label>
<label><input type="radio" name="otazka1" value="n" /> asda</label>

...

<input type="submit" id="continue" value="Continue" />
<script>
    var continue_button = document.getElementById('continue');
    continue_button.onclick = function(event) {
        var everything_OK = true;
        var inputs = document.getElementsByTagName("input");
        for (var i=0; i<inputs.length; i++) {
            if (inputs[i].type != "radio")
                continue;
            var should_be = inputs[i].value == "y";
            if (inputs[i].checked != should_be) {
                everything_OK = false;
                break;
            }
        }
        if (everything_OK) {
            // this is not a good success-page
            window.location.href = "http://www.facebook.com");
        } else {
            window.location.href = "http://www.google.sk";
        }
    };
</script>
是
不
斯普拉夫纳
阿斯达
...
var continue_button=document.getElementById('continue');
continue\u button.onclick=函数(事件){
var everything_OK=true;
var inputs=document.getElementsByTagName(“输入”);
对于(var i=0;i