2017-06-06 12 views
0

私はほとんどこれが働いていますが、私は決して理解できないことがあります。 私がやろうとしているのは、オンザフライでラジオボタンリストを作成することです。しかし、私がこれを行うと、毎回ラジオボックスが作成されるので、すべての回答を選択することができます。 1つの入力を作成するには、どのように4つのオプションがあるので、それらの代わりに1つだけを選択することができますか?彼らは二つ以上の無線機グループに同じ名前を割り当てるオンザフライでラジオボタンリストを作成する

function questionAnswers() { 
    let arr = Object.values(questionList[i].answers) 
    for (var a in arr){ 
    let checkbox = document.createElement('input') 
    let label = document.createElement('label') 
    label.appendChild(document.createTextNode(arr[a])) 
    checkbox.type = 'radio'; 
    answerSection.appendChild(checkbox); 
    answerSection.appendChild(label) 
    } 
} 
+2

ラジオは、名前でグループ化されます。すべて同じ名前を割り当ててください。 –

+0

この[example](https://stackoverflow.com/questions/28543752/multiple-radio-button-groups-in-one-form)は、 – lilixiaocc

答えて

0

var randname=""+Math.floor(Math.random()*10000); 

function questionAnswers() { 
 
    let arr = ["shark","tiger","lion"]; 
 
    let answerSection=document.body; 
 
    for (var a in arr){ 
 
    let checkbox = document.createElement('input') 
 
    let label = document.createElement('label') 
 
    label.appendChild(document.createTextNode(arr[a])) 
 
    checkbox.type = 'radio'; 
 
    checkbox.name="animal"; 
 
    answerSection.appendChild(checkbox); 
 
    answerSection.appendChild(label) 
 
    } 
 
} 
 

 
window.onload=questionAnswers;

を使用すると、複数の無線機を持っている場合は、あなたのようなランダムな名前を割り当てることができは

+0

私はあなたがこの行に追加しただけであることを知ることができます: 'checkbox.name =" animal ";'私は今これをしています。なぜ説明できますか? –

+0

@ Theworm * 2つ以上のラジオに同じ名前を割り当てる* –

0

すべてのラジオボタンに同じ名前を付けます。

function questionAnswers() { 
 
    let arr = Object.values(questionList[i].answers) 
 
    for (var a in arr){ 
 
    let checkbox = document.createElement('input') 
 
    let label = document.createElement('label') 
 
    label.appendChild(document.createTextNode(arr[a])) 
 
    checkbox.type = 'radio'; 
 
    checkbox.name = "myradio"; 
 
    answerSection.appendChild(checkbox); 
 
    answerSection.appendChild(label) 
 
    } 
 
}

関連する問題