OPが彼が述べたように、要件に関して考えられる問題を説明することは十分に重要だと思うので、このjsFiddleを作成して、すべてのラジオボタンに同じ名前を割り当てることと、各ラジオボタンの名前を異なるものに変更することの違いを示しました.
最初の行のラジオ ボタンはすべてチェック可能ですが、オフにするものがないため、チェックを外すことはできません (同じ名前の他のラジオ ボタンでオフに切り替えることはできません)。
2 番目の行のラジオ ボタンは、ラジオ ボタン グループと同じように機能します... 一度にチェックできるラジオ ボタンは 1 つだけです。
http://jsfiddle.net/2ENZP/1/
HTML:
<form name="quiz" method="post" action="evaluate.jsp">
<h3>Buttons with Differing Names</h3>
<fieldset id="buttonsDiffNames">
</fieldset>
<h3>Buttons with Same Names</h3>
<fieldset id="buttonsSameNames">
</fieldset>
<input type="submit">
</form>
Javascript:
var radioBtns = [
{id:0,name:"charles"},
{id:1,name:"diana"},
{id:2,name:"peter"},
{id:3,name:"sofia"},
{id:4,name:"reggie"},
{id:5,name:"jim"}
];
// set up templates for use by the examples, below.
var tmpl = '<input type="radio" name="{ID}" value="{ID}">{NAME}';
var tmpl2 = '<input type="radio" name="{ID}" value="{NAME}">{NAME}';
// This one will populate the first list, with buttons
// that all have different names
function populateWithDifferingNames(){
// get a ref to the fieldset we're going to add these to
var fieldSet = document.getElementById("buttonsDiffNames");
// create an array to act as a StringBuilder
var sb = [];
// loop through your dataset...
for(var i = 0; i < radioBtns.length; i++){
// add a string to the sb array, replacing the templated
// areas with the incremented radio button id and the name
// from the dataset
sb.push(tmpl.replace(/\{ID\}/gi,"q" + radioBtns[i].id).replace("{NAME}", radioBtns[i].name));
}
// set the fieldset's innerHTML to the joined string representation
// of the sb array
fieldSet.innerHTML = sb.join("\n");
}
// Same idea, but with matching names
function populateWithSameNames(){
var fieldSet = document.getElementById("buttonsSameNames");
var sb = [];
for(var i = 0; i < radioBtns.length; i++){
sb.push(tmpl2.replace(/\{ID\}/gi,"q").replace(/\{NAME\}/gi, radioBtns[i].name));
}
fieldSet.innerHTML = sb.join("\n");
}
window.onload = function() {
populateWithDifferingNames();
populateWithSameNames();
}();
</p>