2017-11-30 9 views
0

ラジオボタン用のinnerTextをループする方法はありますか?私は配列内の値を持っているので、私はループしているラジオボタンにテキストとしてループしたい。クラスと値を持つラジオボタンを作成しているfor-llopがあるので、今度はその値をテキストとして使う必要があるそれと一緒に行く。このように:ラジオボタンのDOM innerTextループ

<input type="radio" class="response" value="2">2 

私がこれまで持っているコードは次のとおりです。

let radioButtons = Object.values(questionAlternatives) 

for (let i = 0; i < Object.keys(questionAlternatives).length; i++) { 
    let answerRadio = document.querySelector('#answerRadio') 
    let radioButton = document.createElement('input') 
    radioButton.setAttribute('type', 'radio') 
    radioButton.setAttribute('class', 'response') 
    radioButton.setAttribute('value', radioButtons[i]) 
    answerRadio.appendChild(radioButton) 
} 

それは仕事ができる任意の方法?

for (let i = 0; i < Object.keys(questionAlternatives).length; i++) { 
    let answerRadio = document.querySelector('#answerRadio') 
    let radioButton = document.createElement('input') 
    let radioText = document.createTextNode(radioButtons[i].toString()) 
    radioButton.setAttribute('type', 'radio') 
    radioButton.setAttribute('class', 'response') 
    radioButton.setAttribute('value', radioButtons[i]) 
    answerRadio.appendChild(radioButton) 
    answerRadio.appendChild(radioText) 
} 

答えて

0

は、私はいくつかの掘削後に答えを見つけました
関連する問題