2014-02-14 2 views
1

실제 간단한 폼을 만들고 JS를 사용하여 라디오 버튼의 값에 액세스 한 다음 빠른 계산을 수행하고 함수를 기반으로 팝업 상자에 문자열을 반환합니다. 문제는 버튼을 클릭했을 때 팝업이 문자열이 아닌 호출 된 함수의 코드를 반환한다는 것입니다.자바 스크립트 확인 상자가 함수 값 대신 코드를 반환합니다.

 <div id="questionaire"> 
     <div id="question1">  
      <h2>How familiar are you with Javascript?</h2><br> 
      <form id="first_q"> 
       <input type="radio" name="q1" value="4">Very Experienced.</input><br> 
       <input type="radio" name="q1" value="3">I've Worked with the Basics.</input><br> 
       <input type="radio" name="q1" value="2">I Only Know Its a Programming Language.</input><br> 
       <input type="radio" name="q1" value="1">Java...Mmm, Coffee sounds GOOD!</input> 
      </form> 
     </div> 
     <div id="question2"> 
      <h2>How much do you like to code?</h2><br> 
      <form id="second_q"> 
       <input type="radio" name="q2" value="4">I think about coding during sex!</input><br> 
       <input type="radio" name="q2" value="3">It's safe to say, I take coding pretty_seriously.</input><br> 
       <input type="radio" name="q2" value="2">I would consider it my cold-weather hobby.</input><br> 
       <input type="radio" name="q2" value="1">Technology is cool.</input> 
      </form> 
     </div> 
    </div> 
    <div id="answer"> 
     <button id="color-switcher">TELL ME MORE...</button> 
    </div> 

가 외부 JS

var q1_rating = function() { 
var first_q_answer = document.getElementsByName("q1"); 

for(var i = 0; i < first_q_answer.length; i++) { 
    if(first_q_answer[i].checked == true) { 
    q1_value = first_q_answer[i].value; 
    } 
} 

return parseFloat(q1_value); 
}; 

var q2_rating = function() { 
var second_q_answer = document.getElementsByName("q2"); 

for(var i = 0; i < second_q_answer.length; i++) { 
    if(second_q_answer[i].checked == true) { 
    q2_value = second_q_answer[i].value; 
    } 
} 

return parseFloat(q2_value); 
}; 

var final_rating = function() { 
total_value = (q1_rating + q2_rating); 

if (total_value > 6) { 
    return "You're a TRUE NERD ready to conquer the world."; 
} else if (total_value > 4 && total_value <=6) { 
    return "You need some more work, but you're coming along nicely."; 
} else if (total_value >2 && total_value <=4) { 
    return "You need to step up your nerd game pronto!"; 
} else { 
    return "You're more jock than nerd"; 
} 
}; 

document.getElementById('color-switcher').addEventListener('click', function(){ 
    confirm(final_rating); 
}) 

답변

3

링크 당신은 final_rating 기능

confirm(final_rating()); 

현재, 당신은 함수의 참조

+0

오 남자를 전달하는 호출해야합니다! 확실히 내 기능에 전화해야합니다. 신선한 눈을 가져 주셔서 감사합니다. – Jay

관련 문제