Taking values from Radio button with different names using Javascript
I want to take values from Radio button with different names and store in an 开发者_开发技巧array in javascript I am not able to figure out how to do that. I am able to take value for one name, but not for multiple name radios.
http://jsfiddle.net/zkhj9/1/
If this is what you mean heres a quick example.
JS
var valArr = [],
group1 = document.getElementsByName("g1"),
group2 = document.getElementsByName("g2");
for(var i = 0; i < group1.length; i++){
valArr.push(group1[i].value);
}
for(var i = 0; i < group2.length; i++){
valArr.push(group2[i].value);
}
var result = document.getElementById("result");
result.innerHTML = valArr.toString();
Markup
<h2>Group 1</h2>
<input type="radio" name="g1" value="g1_opt1">opt1</input>
<input type="radio" name="g1" value="g1_opt2">opt2</input>
<br/>
<h2>Group 2</h2>
<input type="radio" name="g2" value="g2_opt1">opt1</input>
<input type="radio" name="g2" value="g2_opt2">opt2</input>
<h2>Result</h2>
<div id="result">
</div>
精彩评论