2017-12-11 23 views
-1

나는 웹 사이트를 만들려고 노력하고 있습니다. 질문을하는 페이지가 있으며 선택한 라디오 버튼에 따라 두 가지 답변 중 하나가 표시됩니다. "예"를 선택하면 실패했음을 알립니다. 5 개를 모두 "아니오"로 선택하면 통과했음을 알려줍니다. 나는 세 가지 질문 만했을 때이 작업을 수행했지만, 두 개를 더 추가하여 총 5 개를 시도하고 코드를 변경하여 작동을 멈추게했습니다. 내가 선택한 선택에 상관없이 대답을주지 못한다. JavaScript가 깨어지기 시작합니다

은 내가 그것을 조정하려고 할 때 깨는 계속 왜 난 정말 이해가 안 돼요,이
function answer(total) { 
    var score = 0; 
    if (document.getElementById('exp_no').checked) { 
     score++; 
    } 
    if (document.getElementById('chg_no').checked) { 
     score++; 
    } 
    if (document.getElementById('mur_no').checked) { 
     score++; 
    } 
    if (document.getElementById('off_no').checked) { 
     score++; 
    } 
    if (document.getElementById('fin_no').checked) { 
     score++; 
    } 
    if (score != 5) { 
     document.getElementById('totalScore').innerHTML = "You may not yet be 
     fully eligible to get your record expunged.Though this test should not be 
     considered as a definite answer.Please call 574 - 931 - 2173 or visit our 
     contact page to schedule a meeting 
     for a proper assessment. 
     "; 
     toggletab(); 
    } else { 
     document.getElementById('totalScore').innerHTML = "You appear to be eligible 
     for expungment!Please contact 574 - 931 - 2173 or visit our contact page to 
     schedule a meeting and to talk to about your next steps!"; 
     toggletab2(); 
    } 
} 

function answer(total) { 
    var score = 0; 
    if (document.getElementById('exp_no').checked) { 
     score++; 
    } 
    if (document.getElementById('chg_no').checked) { 
     score++; 
    } 
    if (document.getElementById('mur_no').checked) { 
     score++; 
    } 
    if (document.getElementById('off_no').checked) { 
     score++; 
    } 
    if (document.getElementById('fin_no').checked) { 
     score++; 
    } 
    if (score != 5) { 
     document.getElementById('totalScore').innerHTML = "You may not yet be 
     fully eligible to get your record expunged.Though this test should not be 
     considered as a definite answer.Please call 574 - 931 - 2173 or visit our 
     contact page to schedule a meeting 
     for a proper assessment. 
     "; 
     toggletab(); 
    } else { 
     document.getElementById('totalScore').innerHTML = "You appear to be eligible 
     for expungment!Please contact 574 - 931 - 2173 or visit our contact page to 
     schedule a meeting and to talk to about your next steps!"; 
     toggletab2(); 
    } 
} 

감사합니다 내 자바 스크립트 인 HTML

<form id="myForm"> 
    <fieldset> 
     <h3> 
      <legend>Eligibility Test</legend> 
     </h3> 
    </fieldset> 

    <p>Please Answer the following questions to determine if you are eligible for expungement.</p> 

    <fieldset> 

     <legend>Have you had your record expunged before?</legend> 
     <input id="exp_yes" type="radio" name="field1" value="0" /> 
     <label> 
      Yes 
     </label> 
     <input id="exp_no" type="radio" name="field1" value="1" /> 
     <label> 
      No 
     </label> 

    </fieldset> 

    <fieldset> 
     <legend>Do you have any charges pending against you?</legend> 
     <input id="chg_yes" type="radio" name="field2" value="0" /> 
     <label> 
      Yes 
     </label> 
     <input id="chg_no" type="radio" name="field2" value="1" /> 
     <label> 
      No 
     </label> 
    </fieldset> 

    <fieldset> 
     <legend>Have any of your convictions ever been for a murder/manslaughter or attempt of the same?</legend> 
     <input id="mur_yes" type="radio" name="field3" value="0" /> 
     <label> 
      Yes 
     </label> 
     <input id="mur_no" type="radio" name="field3" value="1" /> 
     <label> 
      No 
     </label> 

    </fieldset> 

    <fieldset> 
     <legend>Have any of your convictions been for a sex crime or required you to sign up for the sex offender registry?</legend> 
     <input id="off_yes" type="radio" name="field4" value="0" /> 
     <label> 
      Yes 
     </label> 
     <input id="off_no" type="radio" name="field4" value="1" /> 
     <label> 
      No 
     </label> 

    </fieldset> 

    <fieldset> 
     <legend>Do you still owe any fines, fees, or restitution on any of criminal convictions? 
     </legend> 
     <input id="fin_yes" type="radio" name="field5" value="0" /> 
     <label> 
      Yes 
     </label> 
     <input id="fin_no" type="radio" name="field5" value="1" /> 
     <label> 
      No 
     </label> 

    </fieldset> 
    <fieldset id="submitbutton"> 
     <input type="button" id="submit" value="submit" onclick='answer()' /> 
    </fieldset> 
    <div id="first"> 
     <p id="totalScore"></p> 
    </div> 
</form> 

입니다.

+2

"깨는"의미는 무엇입니까? 오류를 붙여 넣을 수 있습니까? – fejanto

답변

2

문제는 .innerHTML 개의 텍스트 문자열에있는 것으로 보입니다. 귀하의 서식 때문에 JavaScript가 첫 번째 텍스트 문자열을 You may not yet be으로 해석 한 다음 fully eligible이 잘못된 JavaScript로 잘못 구문 오류가 발생했습니다. 한 줄에 전체 문자열을 저장

문제를 해결하기 위해 나타납니다 그 두 기능에서 정의되지 않은 한, 내가 toggletab()toggletab2()을 주석 한

function answer(total) { 
 
    var score = 0; 
 
    if (document.getElementById('exp_no').checked) { 
 
    score++; 
 
    } 
 
    if (document.getElementById('chg_no').checked) { 
 
    score++; 
 
    } 
 
    if (document.getElementById('mur_no').checked) { 
 
    score++; 
 
    } 
 
    if (document.getElementById('off_no').checked) { 
 
    score++; 
 
    } 
 
    if (document.getElementById('fin_no').checked) { 
 
    score++; 
 
    } 
 
    if (score != 5) { 
 
    document.getElementById('totalScore').innerHTML = "You may not yet be fully eligible to get your record expunged. Though this test should not be considered as a definite answer. Please call 574 - 931 - 2173 or visit our contact page to schedule a meeting for a proper assessment."; 
 
    //toggletab(); 
 
    } else { 
 
    document.getElementById('totalScore').innerHTML = "You appear to be eligible for expungment! Please contact 574 - 931 - 2173 or visit our contact page to schedule a meeting and to talk to about your next steps!"; 
 
    //toggletab2(); 
 
    } 
 
}
<form id="myForm"> 
 
    <fieldset> 
 
    <h3> 
 
     <legend>Eligibility Test</legend> 
 
    </h3> 
 
    </fieldset> 
 
    <p>Please Answer the following questions to determine if you are eligible for expungement.</p> 
 
    <fieldset> 
 
    <legend>Have you had your record expunged before?</legend> 
 
    <input id="exp_yes" type="radio" name="field1" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="exp_no" type="radio" name="field1" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>Do you have any charges pending against you?</legend> 
 
    <input id="chg_yes" type="radio" name="field2" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="chg_no" type="radio" name="field2" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>Have any of your convictions ever been for a murder/manslaughter or attempt of the same?</legend> 
 
    <input id="mur_yes" type="radio" name="field3" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="mur_no" type="radio" name="field3" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>Have any of your convictions been for a sex crime or required you to sign up for the sex offender registry?</legend> 
 
    <input id="off_yes" type="radio" name="field4" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="off_no" type="radio" name="field4" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>Do you still owe any fines, fees, or restitution on any of criminal convictions? 
 
    </legend> 
 
    <input id="fin_yes" type="radio" name="field5" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="fin_no" type="radio" name="field5" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset id="submitbutton"> 
 
    <input type="button" id="submit" value="submit" onclick='answer()' /> 
 
    </fieldset> 
 
    <div id="first"> 
 
    <p id="totalScore"></p> 
 
    </div> 
 
</form>

참고하여 예.

희망이 도움이됩니다. :)

+0

javascript는 끝 부분을';'과 같은 명령의 끝으로 해석합니다. 끈 속에서도. '\ r \ n'을 사용하여 javascript 문자열 내에서 새 행 (캐리지 리턴, 새 행)을 표시하십시오. –

0

재미와 게임을 위해 나는 Obsidians answer을 조금씩 수정하려고합니다.

Obsidian이 문제를 해결했습니다. 따라서 두 가지 답변 중에서 받아 들여지는 답변의 가치가 있습니다. 문자열은 한 줄에 있어야합니다. Javascript는 문자열 구분 기호 내에서조차 새로운 명령으로 모든 새 행을 처리합니다.

querySelectorAll 메서드를 사용하면 CSS 선택기를 사용하여 찾은 요소의 길이를 비교할 수 있습니다.

function answer(total) { 
 
    var score = 0; 
 
    
 
    //This all seemed like a good idea at the time but there is a better way! 
 
    //Get all our no buttons -> Any radio button with a value of one that is in a fieldset tag. 
 
    //var noButtons = document.querySelectorAll("fieldset input[type='radio'][value='1']"); 
 
    
 
    //Loop through our no buttons keeping a track of the score 
 
    /*for(var i = 0; i < noButtons.length; i++){ 
 
    if(noButtons[i].checked){ 
 
     score++; 
 
    } 
 
    }*/ 
 
    
 
    
 
    //If there are un checked no buttons....uh oh! 
 
    //if (score != noButtons.length) { 
 
    
 
    //The if statme below does the following 
 
    //Find all radio buttons with a value of 1 that are checked and are in a field set and count them. 
 
    //Compare that to the number of radio buttons with a value of radio buttons with a value of 1 that are in a field set. 
 
    if(document.querySelectorAll("fieldset input[type='radio'][value='1']:checked").length != document.querySelectorAll("fieldset input[type='radio'][value='1']").length) { 
 
    document.getElementById('totalScore').innerHTML = "You may not yet be fully eligible to get your record expunged. Though this test should not be considered as a definite answer. Please call 574 - 931 - 2173 or visit our contact page to schedule a meeting for a proper assessment."; 
 
    //toggletab(); 
 
    } else { 
 
    document.getElementById('totalScore').innerHTML = "You appear to be eligible for expungment! Please contact 574 - 931 - 2173 or visit our contact page to schedule a meeting and to talk to about your next steps!"; 
 
    //toggletab2(); 
 
    } 
 
}
<form id="myForm"> 
 
    <fieldset> 
 
    <h3> 
 
     <legend>Eligibility Test</legend> 
 
    </h3> 
 
    </fieldset> 
 
    <p>Please Answer the following questions to determine if you are eligible for expungement.</p> 
 
    <fieldset> 
 
    <legend>Have you had your record expunged before?</legend> 
 
    <input id="exp_yes" type="radio" name="field1" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="exp_no" type="radio" name="field1" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>Do you have any charges pending against you?</legend> 
 
    <input id="chg_yes" type="radio" name="field2" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="chg_no" type="radio" name="field2" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>Have any of your convictions ever been for a murder/manslaughter or attempt of the same?</legend> 
 
    <input id="mur_yes" type="radio" name="field3" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="mur_no" type="radio" name="field3" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>Have any of your convictions been for a sex crime or required you to sign up for the sex offender registry?</legend> 
 
    <input id="off_yes" type="radio" name="field4" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="off_no" type="radio" name="field4" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>Do you still owe any fines, fees, or restitution on any of criminal convictions? 
 
    </legend> 
 
    <input id="fin_yes" type="radio" name="field5" value="0" /> 
 
    <label>Yes</label> 
 
    <input id="fin_no" type="radio" name="field5" value="1" /> 
 
    <label>No</label> 
 
    </fieldset> 
 
    <fieldset id="submitbutton"> 
 
    <input type="button" id="submit" value="submit" onclick='answer()' /> 
 
    </fieldset> 
 
    <div id="first"> 
 
    <p id="totalScore"></p> 
 
    </div> 
 
</form>
기본적으로 추가하거나 질문을 제거 할 경우 쉽게 당신의 인생을 만들어 내가 여기에 완료했습니다. 당신은 가서 다시 코딩 할 필요가 없습니다. 또한 클래스 나 데이터 속성을 CSS로 정의하여 버튼을 정의 할 수도 있습니다.