简体   繁体   English

HTML单选按钮Javascript

[英]HTML Radio Button Javascript

http://jsfiddle.net/8sbt5oua/8/ http://jsfiddle.net/8sbt5oua/8/

I would like to get my code only concentrated on a few everytime. 我想让我的代码每次只专注于少数几个。
Instead of all of them, who could help me? 除了所有人,谁能帮助我?

gif

    <input id="01" type="radio">
    <input id="02" type="radio">
    <input id="03" type="radio">
    <input id="04" type="radio">
    <input id="05" type="radio">
    <input id="06" type="radio">


var btn = document.getElementById("02");
setInterval(function() {
  var list=document.getElementsByTagName("input");
  for (i=0;i<list.length;i++) {
    btn = list[i];
    btn.checked == false ? btn.setAttribute("checked", "checked") : btn.removeAttribute("checked");
  }
}, 500);

Here's the JsFiddle: http://jsfiddle.net/8sbt5oua/10/ 这是JsFiddle: http : //jsfiddle.net/8sbt5oua/10/

First have two arrays that you want to flicker on and off. 首先有两个要闪烁的数组。

var arr1 = [0,1,2];
var arr2 = [3,4,5];

You also want a global variable that toggles between the two. 您还需要一个在两者之间切换的全局变量。

var use_first = true;

Then you want to use the toggling variable to determine which one toggles on and which ones toggles off. 然后,您要使用切换变量来确定哪个打开和关闭。

let remove;
let checked;

if (use_first)
{
    checked = arr1;
  remove = arr2;
}
else
{
    checked = arr2;
  remove = arr1;
}

And I think the foreach explains themselves since they're basically your code. 而且我认为foreach可以自我解释,因为它们基本上就是您的代码。

Edit: 编辑:

remove.forEach(function(item, index)
{
    btn = list[item];
  btn.removeAttribute("checked");
});
checked.forEach(function(item, index)
{
    btn=list[item];
  btn.setAttribute("checked", "checked");
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM