2017-07-26 11 views
1

私はHTMLとJavaスクリプトを使用している新しいセキュリティ評価Webページに取り組んでいます。何らかの理由で、コードが正しく動作していません。ページの動作方法は、ユーザーに「はい」または「いいえ」と答えさせることです。ユーザーが完了すると、「送信」をクリックします。ページにはイエスと答えた質問の数と、ユーザーが評価を合格または不合格にした場合のメッセージが表示されます。しかし、私がsubmitをクリックすると、私は結果だけを見て、メッセージは見ません。メッセージを表示

フィードバックは高く評価されます。

function sumInputs() { 
 
    var text; 
 
    var score = document.getElementById("total").value; 
 

 
    if (score < 60) { 
 
    text = "You have failed the Assessment"; 
 
    } else(score > 60) { 
 
    text = "You have passed the Assessment"; 
 
    } 
 

 
    document.getElementById("demo").innerHTML = text; 
 
} 
 

 
function calcscore() { 
 
    var score = 0; 
 
    $(".calc:checked").each(function() { 
 
    score += parseInt($(this).val(), 10); 
 
    }); 
 
    $("input[name=sum]").val(score) 
 
} 
 

 
$().ready(function() { 
 
    $(".calc").change(function() { 
 
    calcscore() 
 

 
    }); 
 
}); 
 

 
window.sumInputs = function() { 
 
    var inputs = $('.calc:checked'), 
 
    result = document.getElementById('total'), 
 
    sum = 0; 
 

 
    for (var i = 0; i < inputs.length; i++) { 
 
    sum += parseInt(inputs[i].value); 
 
    } 
 

 
    $('#total').val(sum); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 

 
<body> 
 
    <p id="demo"></p> 
 
    <h1>Security Assessment </h1> 
 

 
    <table> 
 
    <tr> 
 
     <th>PERSONNEL SECURITY</th> 
 
    </tr> 
 
    <tr> 
 
     <td>1. Does your staff wear ID badges? 
 
     <form> 
 
      Yes 
 
      <input class="calc" type="radio" name="radio1" value="1" /> No 
 
      <input class="calc" type="radio" name="radio1" value="0" /><br /> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>2. Is a current picture part of the ID badge? Yes 
 
     <input class="calc" type="radio" name="radio2" value="1" /> No 
 
     <input class="calc" type="radio" name="radio2" value="0" /><br /> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>3. Are authorized access levels and type (employee, contractor, visitor) identified on the Badge? Yes 
 
     <input class="calc" type="radio" name="radio3" value="1" /> No 
 
     <input class="calc" type="radio" name="radio3" value="0" /><br /> 
 
     </form> 
 
     </td> 
 
    </tr> 
 
    </table> 
 

 
    Total : <input type="text" name="total" id="total" /> 
 

 
    <input type="Submit" value="Submit" onclick="sumInputs()"> 
 
</body> 
 
</html>

+1

なぜ2つのHTML文書は、 – guest271314

+4

私は構文エラーを修正始めるのでしょうか?質問であるあなたのコンソールを見てください。 – empiric

答えて

0

これが動作するかどうか確認してください。また、あなたの変数の型

function calcscore() { 
 
    var score = 0; 
 
    $(".calc:checked").each(function() { 
 
     score += parseInt($(this).val(), 10); 
 
    }); 
 
    $("input[name=sum]").val(score) 
 
} 
 
    
 
    
 
$().ready(function() { 
 
    $(".calc").change(function() { 
 
     calcscore() 
 
    }); 
 
}); 
 

 

 

 
    
 
function sumInputs() { 
 
    var inputs = $('.calc:checked'), 
 
     result = document.getElementById('total'), 
 
     sum = 0; 
 

 
    for (var i = 0; i < inputs.length; i++) { 
 
     sum += parseInt(inputs[i].value); 
 
    } 
 

 
    $('#total').val(sum); 
 

 
    var text, score = document.getElementById("total").value; 
 

 
    if (score < 60) { 
 
     text = "You have failed the Assessment"; 
 
    } else if(score > 60) { 
 
     text = "You have passed the Assessment"; 
 
    } 
 

 
    document.getElementById("demo").innerHTML = text; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
\t 
 
\t \t <body> 
 

 
\t \t <h1>Security Assessment </h1> 
 

 
\t <table> 
 
    <tr> 
 
    <th>PERSONNEL SECURITY</th> 
 
    
 
    </tr> 
 
    <tr> 
 
    <td>1. Does your staff wear ID badges? 
 

 
<form> 
 
Yes 
 
<input class="calc" type="radio" name="radio1" value="1" /> 
 
No 
 
<input class="calc" type="radio" name="radio1" value="0" /><br /> 
 

 
</form> 
 

 

 
</td> 
 

 
    
 
    </tr> 
 
    <tr> 
 
    <td>2. Is a current picture part of the ID badge? 
 

 

 
<form> 
 
Yes 
 
<input class="calc" type="radio" name="radio2" value="1" /> 
 
No 
 
<input class="calc" type="radio" name="radio2" value="0" /><br /> 
 

 
</form> 
 

 

 
</td> 
 
    
 
    
 
    </tr> 
 
    <tr> 
 
    <td>3. Are authorized access levels and type (employee, contractor, visitor) identified 
 
on the Badge? 
 

 
<form> 
 
Yes 
 
<input class="calc" type="radio" name="radio3" value="1" /> 
 
No 
 
<input class="calc" type="radio" name="radio3" value="0" /><br /> 
 

 
</form> 
 

 
</td> 
 
     
 
    </tr> 
 
    
 
    
 
</table> 
 

 
Total : <input type="text" name="total" id="total"/> 
 

 

 

 
<input type="Submit" value="Submit" onclick="sumInputs()"> 
 

 
<p id="demo"></p> 
 
</body> 
 
</html>

+3

してくださいあなたが変更され、正確に何を説明した。 –

+1

@winDitherあなたの助けをありがとう。今私はメッセージを見ることができますが、評価の結果は見ることができません。 – SHO

0

あなたはsumInputsという名前の2つの機能を持っているを確認します。 (スコアフィールドを満たした)2番目のものが最初のものを上書きしていた(メッセージを出力するはずだった)。

さらに、スコアはパーセントに基づいているようにしようとしていたが、正しい質問の数。

(あちこちで構文エラーのカップルもありました。)

に2 sumInputs機能の結果をマージすることにより、すべてのそれらの修正:

ちなみに

function calcscore() { 
 
    var score = 0; 
 
    $(".calc:checked").each(function() { 
 
    score += parseInt($(this).val(), 10); 
 
    }); 
 
    $("input[name=sum]").val(score) 
 
} 
 

 
$().ready(function() { 
 
    $(".calc").change(function() { 
 
    calcscore() 
 
    }); 
 
}); 
 

 
window.sumInputs = function() { 
 
    var inputs = $('.calc:checked'), 
 
    result = document.getElementById('total'), 
 
    sum = 0; 
 

 
    for (var i = 0; i < inputs.length; i++) { 
 
    sum += parseInt(inputs[i].value); 
 
    } 
 

 
    $('#total').val(sum); 
 
    
 
    var score = sum/3 * 100; // calculate percentage 
 

 
    if (score < 60) { 
 
    text = "You have failed the Assessment"; 
 
    } else { // no need for else if (score > 60)... which would have failed if the score was exactly 60 
 
    text = "You have passed the Assessment"; 
 
    } 
 
    document.getElementById("demo").innerHTML = text; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 

 
<body> 
 
    <p id="demo"></p> 
 
    <h1>Security Assessment </h1> 
 

 
    <table> 
 
    <tr> 
 
     <th>PERSONNEL SECURITY</th> 
 
    </tr> 
 
    <tr> 
 
     <td>1. Does your staff wear ID badges? 
 
     <form> 
 
      Yes 
 
      <input class="calc" type="radio" name="radio1" value="1" /> No 
 
      <input class="calc" type="radio" name="radio1" value="0" /><br /> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>2. Is a current picture part of the ID badge? Yes 
 
     <input class="calc" type="radio" name="radio2" value="1" /> No 
 
     <input class="calc" type="radio" name="radio2" value="0" /><br /> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>3. Are authorized access levels and type (employee, contractor, visitor) identified on the Badge? Yes 
 
     <input class="calc" type="radio" name="radio3" value="1" /> No 
 
     <input class="calc" type="radio" name="radio3" value="0" /><br /> 
 
     </form> 
 
     </td> 
 
    </tr> 
 
    </table> 
 

 
    Total : <input type="text" name="total" id="total" /> 
 

 
    <input type="Submit" value="Submit" onclick="sumInputs()"> 
 
</body> 
 

 
</html>

calcscore()関数は不要です - ユーザーがクイズを取っている間に表示されたスコアを更新しますが、クイズの最後に使用されたsumInputs関数はそれに依存しません。ユーザーに各質問が正しいかどうかのフィードバックをすぐに伝えたくない場合は、その機能を削除してsumInputs()に移動してsumInputs()に移動してください。

0

私はいくつかの修正を加えてプレーンなjavascriptをjqueryの。

$('.calc').change(function() { 
 
    calcscore();    
 
}); 
 

 
$('#submitted').click(function() { 
 

 
    sumInputs(); 
 
    result(); 
 

 
}); 
 

 
function sumInputs() { 
 

 
    var inputs = $('.calc:checked'), 
 
     result = $('#total').val(), 
 
     sum = 0; 
 

 
    for (var i = 0; i < inputs.length; i++) { 
 
     sum += parseInt(inputs[i].value); 
 
    } 
 

 
    $('#total').val(sum); 
 

 
} 
 

 
function result() { 
 

 
    var text; 
 
    var score = (($('#total').val())/3) * 100; 
 
    var score = score.toFixed(2);    
 

 
    if (score < 60) { 
 
     text = "Total " + score + ": You have failed the Assessment"; 
 
    } 
 
    else {    
 
     text = "Total " + score + ": You have passed the Assessment"; 
 
    } 
 

 
    $('#demo').text(text); 
 
} 
 

 
function calcscore() { 
 

 
    var score = 0; 
 

 
    $('.calc:checked').each(function() {     
 
     score += parseInt($(this).val(), 10); 
 
    }); 
 

 
    $('#total').val(score); 
 

 
}
<html> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<body> 
 
    <h1>Security Assessment </h1> 
 

 
    <form>   
 
    <table> 
 
     <tr> 
 
      <th>PERSONNEL SECURITY</th> 
 
     </tr> 
 
     <tr> 
 
      <td>1. Does your staff wear ID badges? 
 
       Yes <input class="calc" type="radio" name="radio1" value="1" /> 
 
       No <input class="calc" type="radio" name="radio1" value="0" /><br /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>2. Is a current picture part of the ID badge? 
 
       Yes <input class="calc" type="radio" name="radio2" value="1" /> 
 
       No <input class="calc" type="radio" name="radio2" value="0" /><br /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>3. Are authorized access levels and type (employee, contractor, visitor) identified on the Badge? 
 
       Yes <input class="calc" type="radio" name="radio3" value="1" /> 
 
       No <input class="calc" type="radio" name="radio3" value="0" /><br /> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
    </form> 
 

 
    Total : <input type="text" name="total" id="total" /> 
 
    <input id="submitted" type="Submit" value="Submit"><br><br> 
 
    <p id="demo"></p> 
 
    
 
</body> 
 
</html>

関連する問題