Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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/1/ms-access/4.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_Variables_Radio Button_Html - Fatal编程技术网

单选按钮数组和JavaScript函数

单选按钮数组和JavaScript函数,javascript,variables,radio-button,html,Javascript,Variables,Radio Button,Html,为学校作业制作JavaScript游戏,我需要一些帮助 我有一个5x5网格的单选按钮,一个开始按钮和一个表格,里面有一个显示点的跨度。单击开始按钮后,变量“播放”变为true,并选择一个随机电台。如果播放机单击所选收音机,则points变量为+1,并选择另一个随机收音机 到目前为止,我已经设法让随机无线电选择功能工作,但我真的不知道如何去休息。不太确定如何检查玩家是否点击了右单选按钮 function SelectRadio(){ var array = document.getElem

为学校作业制作JavaScript游戏,我需要一些帮助

我有一个5x5网格的单选按钮,一个开始按钮和一个表格,里面有一个显示点的跨度。单击开始按钮后,变量“播放”变为
true
,并选择一个随机电台。如果播放机单击所选收音机,则points变量为
+1
,并选择另一个随机收音机

到目前为止,我已经设法让随机无线电选择功能工作,但我真的不知道如何去休息。不太确定如何检查玩家是否点击了右单选按钮

function SelectRadio(){
    var array = document.getElementsByName('radio'); //Selects all the radio buttons.
    var randomNumber=Math.floor(Math.random()*25)+1; // Selects a random number.
    array[randomNumber-1].checked = true; //Selects a random from the array.
}

我刚做了这个JSFIDLE,看看它,然后告诉你是否需要其他东西

var array=document.getElementsByName('radio');
var currentlyCheckedRadio;
var点=0;
对于(var i=0;i
我同意@Frits,我只是注意到这是一个家庭作业,并添加了家庭作业标签,我会保留我的答案,但我强烈建议你理解这个想法,而不仅仅是复制粘贴。我会根据需要更改它,而不是问所有的问题,只是我自己无法解决的问题。。。另外300行php和html是简单的xD
var array = document.getElementsByName('radio');
var currentlyCheckedRadio;
var points = 0;

for(var i = 0; i < array.length; i++) {
    array[i].onclick = function() {
        if(this == currentlyCheckedRadio) { // if clicked right one, increment points
            points = points + 1;
            alert('Yay, points: ' + points);
            SelectRadio(); // check another random
        } else {
            return false; // otherwise do nothing
        }
    };
}

function SelectRadio() {
    // Selects a random number.
    var randomNumber=Math.floor(Math.random()*25);

    currentlyCheckedRadio = array[randomNumber];

    //Selects a random from the array.
    currentlyCheckedRadio.checked = true;
}

SelectRadio();