2016-06-16 2 views
0

JS 퀴즈를 만들었으며 배열에서 모든 대답을 선택할 수 있습니다. 대답 선택은 생성 된 <input type="radio" . . . . /> 요소에 삽입됩니다. 그러나 선택 사항은 레이블 내부에있는 것이 아니라 입력 요소 내부에만 있습니다.jQuery를 사용하여 레이블이있는 라디오 삽입

// Creates a list of the answer choices as radio inputs 
    function createRadios(index) { 
    var radioList = $('<ul>'); 
    var item; 
    var input = ''; 
    for (var i = 0; i < questions[index].choices.length; i++) { 
     item = $('<li>'); 
     input = '<input type="radio" name="answer" value=' + i + ' />'; 
     input += questions[index].choices[i]; 
     item.append(input); 
    radioList.append(item); 
    } 
    return radioList; 
    } 
: 입력을 생성하는 코드는 내가 여기이

html-code2

처럼되고 싶어

html-code1

것 : 여기에 더 내가 무슨 말 확장의

도움을 주시면 대단히 감사하겠습니다.

+0

: http://stackoverflow.com/questions/16973457/how-to-dynamically-create-a-form-element – jsanchezs

답변

1

레이블에는 모범 사례를위한 라디오 버튼이 포함되어야합니다. 이 시도 :이 도움이 될 수

// Creates a list of the answer choices as radio inputs 
    function createRadios(index) { 
    var radioList = $('<ul>'); 
    for (var i = 0; i < questions[index].choices.length; i++) { 
     var item = $('<li>'); 
     var label = $('<label>'); 
     var input = $('<input>', {type: 'radio', name: 'answer', value: i}); 
     input.appendTo(label); 
     label.append(questions[index].choices[i]); 
     item.append(label); 
     radioList.append(item); 
    } 
    return radioList; 
    } 
+1

와우! 정말 고맙습니다!! –

관련 문제