2013-10-28 2 views
0

라디오 버튼에서 내 가치를 얻을 수있는 방법을 찾고 있습니다. 여기 html이 있습니다.Jquery로 라디오에서 값 받기

<div id="main_"> 
    <div class="facts_div"> 
     <span class="question"></span> 
     <ul></ul> 
    </div> 
    <div id = "next_button"> 
    <form> 
     <input id="x" type="button" class="myBtn" value="Press Me"> 
    </form> 
    </div> 
</div> 
</div> 

다음은 Jquery 코드입니다. //

var answers = 
[["Fee","Fi","Fo"], 
["La","Dee","Da"]], 
questions = 
["Fee-ing?", 
"La-ing?"], 
corAns = ["Fee", "La"]; 

var counter = 0; 
var correctAnswers = 0; 

var $facts = $('#main_ .facts_div'), 
$question = $facts.find('.question'), 
$ul = $facts.find('ul'), 
$btn = $('.myBtn'); 


$btn.on('click', function() { 
    if (counter < questions.length) { 
     $question.text(questions[counter]); 

     var ansstring = $.map(answers[counter], function(value) { 
      return '<li><input type="radio" name="ans" value="0"/>' 
      + value + '</li>'}).join(''); 
     $ul.html(ansstring); 
     alert($('input[name="ans"]:checked').val()) 
     //if ($('input[name=ans]:checked').val() == corAns[counter]) { 
      //correctAnswers++; 
     //} 
    } 
    else { 
     $facts.text('You are done with the quiz ' + correctAnswers); 
     $(this).hide() 
    } 
    counter++; 
}); 



//  
}); 

당신이 말할 수

$(document).ready (function() { 

, 그것은 undefined를 반환합니다 계속, 나는 내가 잘못 무엇을 아주 확실하지 않다.

덧붙여 말하자면,이 값은 내가 퀴즈에서 옳거나 틀린 대답인지 여부를 측정하는 것입니다. 따라서 결국 if 문에 array[counter]을 사용해야합니다. 그러나, 나는 이것을 분리하여 시험하고 비틀고 값을 얻는 방법을 알아 냈습니다.

+0

페이지로드시 아무 것도 확인되지 않습니까? – tymeJV

+0

나는 그렇게하는 법을 모릅니다. –

+2

코드에서 페이지로드시 선택된 라디오 버튼 값을 경고하지만로드가 확인되지 않으므로 undefined가 수신됩니다 – tymeJV

답변

0

나는 당신이 이것을 필요한 것보다 훨씬 복잡하게 만들고 있다고 생각합니다. 당신은 단순히 정답의 배열을해야하고, 다음 버튼을 단지 루프를 클릭하고 답을 확인 (당신이 코멘트에 놀이로 놀이를 볼 수 있습니다) :

물론
$btn.on('click', function() { 
    //Your answers array should never be longer than your questions length, so that check is a bit useless, scrap it 
    //Lets first gather all your answers into an array, the .map function is built for this: 

    var currentAnswers = $('input[name="ans"]:checked').map(function() { 
     return this.value; 
    }).get(); 

    //Now that we have all the checked radio buttons from the quiz, loop and compare to your answers array, if they match, do something 
    //You have a 2D array of answers, then one of correct answers, you only need 1 array, and that should contain the correct responses, so eliminate the 2D array 
    //Your correct answers should be: corAns = ["Fee", "La"]; 

    var correct = 0; //current number of correct answers 
    for (var i = 0; i < currentAnswers.length; i++) { 
     if (currentAnswers[i] == corAns[i]) { 
      correct++; //Increment correct variable 
     } 
    } 
}); 

당신은 확인하기 위해 검사를 할 수 있습니다 사용자가 모든 것을 응답했는지 아니면 결국 배열이 범위를 벗어나 오류를 일으킬 수 있는지 확인하십시오.

+0

안녕하세요. 감사합니다. 내 의견에 스너키 소리가 들린다면 미안하다. 그것은 내 의도가 아니었다. 나는 아주 오랫동안이 일을하지 않고있다. 때로는 다소 실망 스럽습니다 (좋은 방법으로). 이 방법은 훨씬 더 우아합니다. –

+0

개발은 그런 식으로 일을 처리 할 수있는 다양한 방법이 있습니다. 계속해서 질문을 계속하십시오! 도와 줄 수있어서 기뻐! - @ JerryWalker – tymeJV

+0

질문이 하나 더 있습니다 ... 전체 퀴즈에 대한 배열이 하나만있는 경우 어떻게 모든 대답을 동적으로 추가 할 수 있습니까? 표시된 유일한 답은 사실 올바른 대답 일 것입니다. –