Javascript 我们如何洗牌一系列单选按钮,以便随机单击一个

Javascript 我们如何洗牌一系列单选按钮,以便随机单击一个,javascript,arrays,for-loop,radio-button,shuffle,Javascript,Arrays,For Loop,Radio Button,Shuffle,我有一套单选按钮,在我们的测试过程中,我们必须以某种随机方式手动点击单选按钮 它不必是单击的任何特定单选按钮;但是,在每一行中,必须选择顶部或底部收音机 我用下面的JS代码试过了,但在哪里都没有 救命,救命 这是我的代码: setTimeout(函数(){ var radios=document.body.querySelectorAll('input[type=“radio”]”), 第一个=假; 对于(变量i=0;i

我有一套单选按钮,在我们的测试过程中,我们必须以某种随机方式手动点击单选按钮

它不必是单击的任何特定单选按钮;但是,在每一行中,必须选择顶部或底部收音机

我用下面的JS代码试过了,但在哪里都没有

救命,救命

这是我的代码:

setTimeout(函数(){
var radios=document.body.querySelectorAll('input[type=“radio”]”),
第一个=假;
对于(变量i=0;i
使用fisher-yates shuffle。不要实现你自己的


您的单选按钮名称错误。

如果要选择顶部或底部收音机,则应以不同的方式命名收音机。按照现在的方式,第一行中的所有收音机都在一个组中,而第二行则在另一个组中。因此,您最终只能在每个“行”中选择一个,而不是像您预期的那样在每个“列”中选择一个

HTML代码:-

<table>
  <tr> <td>
      <input type="radio" name = "one">
      <input type="radio" name = "two">
      <input type="radio" name = "three">
      <input type="radio" name = "four">            
      <input type="radio" name = "five">
    </td></tr>
    <tr> <td>
      <input type="radio" name = "one">
      <input type="radio" name = "two">
      <input type="radio" name = "three">
      <input type="radio" name = "four">            
      <input type="radio" name = "five">
    </td></tr>      
</table>

使用JSFIDLE

以下是在Plunker上实现的代码: 重要的代码在script.js中。它将在页面加载时运行,或者您可以运行随机发生器,并通过单击“再次运行”按钮查看所选的随机单选按钮。 我只放了4台收音机在那里,你可以很容易地做更多


(功能(){
随机选择();
})();
函数randomSelect()
{
//取消选择全部
var-allSelectors=['11','12','21','22'];
用于所有选择器中的(s)
{
$(“#”+所有选择器].prop('checked',false);
}
var x=Math.floor(Math.random()*2)+1
变量y=Math.floor(Math.random()*2)+1
控制台日志(y);
var选择器=“#”+x+y;
控制台日志(选择器);
$(选择器).prop('checked',true);
}

什么是“[Fisher Yates]洗牌”?为什么要使用它?为什么OP不应该“实施他自己的”?这个费舍尔·耶茨洗牌应该如何使用?如前所述,虽然我很感激你还不能在任何地方发表评论,但这似乎是一个评论,或者——充其量——一个只有链接的答案,没有有用的解释。任何武断的RNG都是不够的吗?老兄,这个页面比问题更让人困惑
<table>
  <tr> <td>
      <input type="radio" name = "one">
      <input type="radio" name = "two">
      <input type="radio" name = "three">
      <input type="radio" name = "four">            
      <input type="radio" name = "five">
    </td></tr>
    <tr> <td>
      <input type="radio" name = "one">
      <input type="radio" name = "two">
      <input type="radio" name = "three">
      <input type="radio" name = "four">            
      <input type="radio" name = "five">
    </td></tr>      
</table>
    function getRandom(){
      if(Math.random() > 0.5)
        return 1;
      else 
        return 0;  
    }

    document.getElementsByName("one")[getRandom()].checked = true;
    document.getElementsByName("two")[getRandom()].checked = true;
    document.getElementsByName("three")[getRandom()].checked = true;
    document.getElementsByName("four")[getRandom()].checked = true;
    document.getElementsByName("five")[getRandom()].checked = true;
 <table>
      <tr><td><input type="radio" id="11" /><input type="radio" id="12"  /></td></tr>
      <tr><td><input type="radio" id="21" /><input type="radio" id='22' /></td></tr>

    </table>

(function(){
randomSelect();

})();

function randomSelect()
{
  // unselect all
  var allSelectors = ['11', '12', '21', '22'];
  for (s in allSelectors)
  {
    $("#"+allSelectors[s]).prop('checked',false);
  }
var x = Math.floor(Math.random() * 2) + 1  
var y = Math.floor(Math.random() * 2) + 1  
console.log(y);

var selector = "#"+x+y;
console.log(selector);
$(selector).prop('checked', true); 
}