2017-12-11 21 views
-1

私はウェブサイトを作ろうとしています。それは質問をするページがあり、選択されたラジオボタンに応じて2つの回答のうちの1つが表示されます。 「はい」を選択すると、失敗したことが通知されます。 5つすべてを「いいえ」として選択すると、合格したことが伝えられます。私は3つの質問だけがあったが、2つを合計して合計5つを追加しようとしたときにコードを変更して動作しなくなったときに、この作業を行った。私が選択した選択肢に関係なく、それは私に答えを与えることはありません。JavaScriptが破損し続ける

この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> 

これは私の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(); 
    } 
} 

ですが、それは私がそれを調整しようとすると、破壊し続ける理由を、私は本当に理解していない、ありがとう。

+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は問題を解決しましたが(私たちの2つの回答のうち、受け入れられた回答栄誉に値する)。文字列は1行にする必要があります。 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で定義してボタンを定義することもできます。

関連する問題