开发者

How to have a radio button select another radio button's value

开发者 https://www.devze.com 2023-01-23 15:25 出处:网络
I was wondering if there is a way to make a radio button select another radio buttons data. For example, a \"select all\" radio button, would select another radio button. Or In the event of a survey w

I was wondering if there is a way to make a radio button select another radio buttons data. For example, a "select all" radio button, would select another radio button. Or In the event of a survey where a question could be rated from 0 to 5, when "select all as 0" 开发者_JAVA百科is selected, all the following radio buttons are selected at "0". I feel like there is an easy way of doing this, but I have no Idea what it would be (possibly javascript?) Thanks!


HTML

<button type='button' onclick='selectAs0()'>Select All As 0</button>

<input type='radio' name='setA' value='0' /> 0
<input type='radio' name='setA' value='1' /> 1

<input type='radio' name='setB' value='0' /> 0
<input type='radio' name='setB' value='1' /> 1

JS

function selectAs0() {
  var ALL = document.getElementsByTagName("INPUT");

  for(var x = 0; x< ALL.length;x++) {
    if(ALL[x].type=='radio' && ALL[x].value == 0) {
      ALL[x].checked = true;
    }
  }
}

Untested at this point but should work-ish.

// EDIT

If you're determined to use a radio button to actuate the function, the same concept will work...

<input type='radio' onclick='selectAs0()' />

...but you'll have to add extra code to de-check the radio once it's already selected.


Without doing all the work for you, the basic idea would be to have the "select all 0" button (or checkbox, as ThiefMaster said) with an onchange attribute set to a javascript routine, which then requests document.getElementsByTagName("input"), looping through the returned list and setting the "checked" attribute of the zero-valued radio-buttons. I can elaborate if necessary, but that ought to get you started.

0

精彩评论

暂无评论...
验证码 换一张
取 消