2017-12-31 25 views
0
function buildQuiz(currentIndex) { 

    if (currentIndex === 0) { 
    quiz.forEach((item, index) => { 
     outputAnswers = []; 
     for (option in item.option) { 
     outputAnswers.push(
      `<label> <input type="radio" name="question${index}" value="${option}"> 
             ${item.option[option]} </label><br>` 
     ); 
     } 

     // add this question and its answers to the output 
     output.push(
     `<div class="slide${index}"> 
      <div class="question"> ${item.question} </div> 
      <div class="answers"> ${outputAnswers.join("")} </div> 
     </div>` 
    ); 
    }); 
    } 

JavaScript로 퀴즈 응용 프로그램을 만들고 루핑하여 하나씩 "div"를 추가했으며 개별 "div"에는 라디오 버튼과 함께 질문과 옵션이 포함되어 있습니다. 나는 라디오 버튼을 실제로 읽는 것에 대해 고심하고있다.라디오 버튼이 HTML이 아닌 Javascript를 통해 선언되면 Javascript에서 라디오 버튼을 읽는 방법이 선택되었는지 여부가 확인됩니까?

+0

당신은 변수 퀴즈 또는 더미 데이터에 대한 데이터를 제공 할 수 있습니다 ... 가능하면 변수 퀴즈에 대한 다른 니펫을 작업 데이터 만 제공합니다 ... –

+0

여기 jquery를 사용하고 있습니까? 라이브러리는 무엇입니까? –

+0

https://csunix.mohawkcollege.ca/~000736376/a4/ –

답변

0

당신은 라디오 버튼에 클래스를 지정하고 클릭/변경 핸들러를 등록 할 수 있습니다 : 이제

document.querySelectorAll(".your-class") 
    .addEventListener("click", function(e) { 
    // `e.target` will point to the radio button clicked 
    }); 

, 당신은 정확히 식별하기 위해 name 속성을 관찰 한 후 클릭 된 라디오 버튼을 얻을 수 있습니다.

0

이 방법을 사용할 수 있습니다

if(your_radio.checked){ 
    // Do something 
} 
else if(!your_radio.checked){ 
    // Do something 
}