2013-04-27 3 views
0

안녕하세요 얘들 아, 자바 스크립트 만 사용하여 라디오 버튼을 선택하는 방법을 알아 내려고하는 중입니다 ... 일부 jquery를 사용하면 더 쉬울 것이라고 알고 있지만 자바 스크립트는 지금 .. 나는 이름 (getElementsByName)에 의해 요소의 값을 얻고 거기에서 그것을 결정할 수 있다는 것을 안다. 그러나 어떻게 든 그것은 일하지 않는다. ... 여기는 나의 JS 피들이다. http://jsfiddle.net/sean3200/LdNCT/ 그리고 아래는 mycode 예다. .. 고맙다! !!!어떤 라디오 버튼이 자바 스크립트에서 선택되었는지 확인하는 방법

var questions = { 

         allQuestions : [ 


        { 
         topQuestion:["Click on which producer produced Justins Timberlake 4th Album?", "What was the first disney movie Justin Timberlake firsr scored?", "What famous celebrity did Justin Timberlake dated ?", "Star Wars"], 
        },  
        { 
         question: "Select which movie did Justin Timberlake film score in 2008?", 
         choices:["Shark Tank", "The Incredibles", "Finding Memo", "Star Wars"], 
         correctAnswer:3 
        }, 
        { 

         question:"What city was Justin Timberlake born in?", 
         choices: ["Chicago", "Detroit", "Tenessee", "New York"], 
         correctAnswer:3 
        }, 
        { 

         question:"At the age of 11, what famous show did Justin Timberlake appeared on?", 
         choices: ["American Idol", "Family Fued", "Star Search", "The Voice"], 
         correctAnswer:3 
        }, 
        { 

         question:"What hit single did Justin Timberlake perform at the MTV Awards in 2002", 
         choices: ["Sexy Love", "Cry Me A River", "Like I Love You", "What Comes Around"], 
         correctAnswer:3 
        }, 
        { 

         question:"What boy band was Justin Timberlake apart of?", 
         choices: ["One Direction", "Black Street Boys", "98 Degrees", "NSync"], 
         correctAnswer:4 
        }  
         ] 
        }; 

        var newQues = Object.create(questions); 


        for (var i = 0; i < 4; i++){ 


        container = document.getElementById("container"); 
        list = document.getElementById("list"); 
        var li = document.createElement("input");  
        li.type = 'radio'; 
        li.name= 'radio_group';  
        li.id = 'id1'; 
        li.value = newQues.allQuestions[1].correctAnswer;       

        document.body.appendChild(li); 
        el = document.createElement("div"); 
        text = document.createTextNode(newQues.allQuestions[1].choices[i]) 
        list.appendChild(el); 
        el.appendChild(li); 
        el.appendChild(text);       

        } 


      var radios = document.getElementsByName("radio_group"); 
       for (var i = 0; i < radios.length; i++) { 
       if (radios[i].checked) {      
       alert(radios[i].value)       
        } 
       }; 
+0

은 엘 = document.createElement'에서 요소의 어떤 종류를됩니다 이제 작동 하나를 추가 엘 ")'? – RobG

+0

@RobG - 엘 엘 요소는 (li) 인 입력 요소를 보유하고 문서 본문에 추가합니다. - >>> el.appendChild (il) –

+0

죄송합니다, 저는 둔한 상태였습니다. * createElement *의 인수는 태그 이름이어야하며 "el"은 HTML 문서의 유효한 태그 이름이 아닙니다. – RobG

답변

0

데모 : 라디오 버튼에 대한 더의 EventListener이 없습니다 http://jsfiddle.net/LdNCT/2/

, 나는 "(이

radios[i].addEventListener('change', function() { 
    alert(this.value); 
}); 
+0

고맙습니다. @ XCritics! –

관련 문제