2017-02-24 12 views
0

私は、正しい答えをチェックし、ページの下に出力するスコアを数える簡単なクイズを作りたいと思います。私は単純なjsとhtmlでそれを行うことができましたが、jqueryを使って同じことをしたいと思います。しかし、私はjqueryのバージョンでは何も起こることはありません(私はjQueryと傾きに何が間違っているかを見つけるためにnoobです)を提出する前におかげで!jqueryの問題を含む複数のラジオボタンの呼び出し

//Question One Answer is 'a' 
 
//Question One Answer is 'b' 
 
//Question One Answer is 'b' 
 
//Question One Answer is 'b' 
 
//Question One Answer is 'a' 
 

 
var score = 0; 
 

 
checkAll = function() { 
 

 
    var message; 
 
    // var score = 0; 
 

 
    if ($("#question1").val() === "a") { 
 
    score++; 
 
    } else { 
 
    return false; 
 
    } 
 

 
    if ($("#question2").val() === "b") { 
 
    score++; 
 
    } else { 
 
    return false; 
 
    } 
 

 
    if ($("#question3").val() === "b") { 
 
    score++; 
 
    } else { 
 
    return false; 
 
    } 
 

 
    if ($("#question4").val() === "b") { 
 
    score++; 
 
    } else { 
 
    return false; 
 
    } 
 

 
    if ($("#question5").val() === "a") { 
 
    score++; 
 
    } else { 
 
    return false; 
 
    } 
 

 
    message = "You got " + score + "out of five questions right!"; 
 
    $("#results").html(message); 
 

 
    initialize = function() { 
 
    $("#init").click(checkAll) 
 
    }; 
 
};
<!doctype html> 
 
<html> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Form Example</title> 
 

 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> 
 
    </script> 
 

 
    <script type="text/javascript" src="quiz.js"></script> 
 

 
</head> 
 

 
<body> 
 

 
    <h1>Geography Quiz</h1> 
 

 

 
    <p> 
 
    <label for="user">Name:</label> 
 
    <input type="text" name="username" id="user" /> 
 
    </p> 
 
    <div class="qheader"> 
 
    <p> 1) Which of the countries below is largest by area?</p> 
 
    </div> 
 
    <div class="questsel"> 
 
    <input type="radio" value="a" name="question1">a) Russia<br> 
 
    <input type="radio" value="b" name="question1">b) China<br> 
 
    <input type="radio" value="c" name="question1">c) USA<br> 
 
    <input type="radio" value="d" name="question1">d) Canada<br> 
 
    </div> 
 

 
    <br> 
 

 
    <div class="qheader"> 
 
    <p> 2) Which of these cities is United States Capital?</p> 
 
    </div> 
 
    <div class="questsel"> 
 
    <input type="radio" value="a" name="question2">a) New York<br> 
 
    <input type="radio" value="b" name="question2">b) Washington D.C 
 
    <input type="radio" value="c" name="question2">c) Chicago<br> 
 
    <input type="radio" value="d" name="question2">d) Moscow<br> 
 
    </div> 
 

 
    <div class="qheader"> 
 
    <p> 3) Which of these cities is Spain's Capital?</p> 
 
    </div> 
 
    <div class="questsel"> 
 
    <input type="radio" value="a" name="question3">a) Barcelona<br> 
 
    <input type="radio" value="b" name="question3">b) Madrid <br> 
 
    <input type="radio" value="c" name="question3">c) Milan<br> 
 
    <input type="radio" value="d" name="question3">d) Rome<br> 
 
    </div> 
 

 
    <div class="qheader"> 
 
    <p> 4) Which ocean is the largest?</p> 
 
    </div> 
 
    <div class="questsel"> 
 
    <input type="radio" value="a" name="question4">a) Arctic<br> 
 
    <input type="radio" value="b" name="question4">b) Pacific<br> 
 
    <input type="radio" value="c" name="question4">c) Indian<br> 
 
    <input type="radio" value="d" name="question4">d) Atlantic<br> 
 
    </div> 
 

 
    <div class="qheader"> 
 
    <p> 5) Which of these rivers is largest in North America?</p> 
 
    </div> 
 
    <div class="questsel"> 
 
    <input type="radio" value="a" name="question5">a) Missouri River<br> 
 
    <input type="radio" value="b" name="question5">b) Nile River <br> 
 
    <input type="radio" value="c" name="question5">c) Amazon River<br> 
 
    <input type="radio" value="d" name="question5">d) Yenisei River<br> 
 
    </div> 
 

 
    <p> 
 
    <input type="button" id="init" value="Submit" /> 
 
    </p> 
 

 
    <p id="results"></p> 
 

 
    </form> 
 

 
</body> 
 

 
</html>

答えて

0

まず、あなたはcheckAll()関数内でごinitialize()機能を持っています。これは別の機能でなければなりません。

第2に、決してinitialize()に電話をかけることはありません。

第3に、$("#question1").val()を使用してラジオボタンの値を取得しますが、#は、名前ではなくIDを照合するためのものです。ラジオボタンの値を取得する方法は$(":radio[name=question1]:checked").val()です(jQuery get value of selected radio button参照)。

第4に、答えが間違っている場合はすぐに戻ります。あなたは合計スコアを報告できるように続ける必要があります。

第5に、関数内でscore0を初期化する必要があります。それ以外の場合は、前の時間のスコアに現在のコールのスコアを追加します。 scoreは、関数のローカル変数にする必要があります。グローバル変数は必要ありません。

//Question One Answer is 'a' 
 
//Question One Answer is 'b' 
 
//Question One Answer is 'b' 
 
//Question One Answer is 'b' 
 
//Question One Answer is 'a' 
 

 

 
checkAll = function() { 
 

 
    var message; 
 
    var score = 0; 
 

 
    if ($(":radio[name=question1]:checked").val() === "a") { 
 
    score++; 
 
    } 
 

 
    if ($(":radio[name=question2]:checked").val() === "b") { 
 
    score++; 
 
    } 
 

 
    if ($(":radio[name=question3]:checked").val() === "b") { 
 
    score++; 
 
    } 
 

 
    if ($(":radio[name=question4]:checked").val() === "b") { 
 
    score++; 
 
    } 
 

 
    if ($(":radio[name=question5]:checked").val() === "a") { 
 
    score++; 
 
    } 
 

 
    message = "You got " + score + " out of five questions right!"; 
 
    $("#results").html(message); 
 

 
}; 
 

 
initialize = function() { 
 
    $("#init").click(checkAll) 
 
}; 
 

 
initialize();
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> 
 
</script> 
 

 

 
<h1>Geography Quiz</h1> 
 

 

 
<p> 
 
    <label for="user">Name:</label> 
 
    <input type="text" name="username" id="user" /> 
 
</p> 
 
<div class="qheader"> 
 
    <p> 1) Which of the countries below is largest by area?</p> 
 
</div> 
 
<div class="questsel"> 
 
    <input type="radio" value="a" name="question1">a) Russia<br> 
 
    <input type="radio" value="b" name="question1">b) China<br> 
 
    <input type="radio" value="c" name="question1">c) USA<br> 
 
    <input type="radio" value="d" name="question1">d) Canada<br> 
 
</div> 
 

 
<br> 
 

 
<div class="qheader"> 
 
    <p> 2) Which of these cities is United States Capital?</p> 
 
</div> 
 
<div class="questsel"> 
 
    <input type="radio" value="a" name="question2">a) New York<br> 
 
    <input type="radio" value="b" name="question2">b) Washington D.C 
 
    <input type="radio" value="c" name="question2">c) Chicago<br> 
 
    <input type="radio" value="d" name="question2">d) Moscow<br> 
 
</div> 
 

 
<div class="qheader"> 
 
    <p> 3) Which of these cities is Spain's Capital?</p> 
 
</div> 
 
<div class="questsel"> 
 
    <input type="radio" value="a" name="question3">a) Barcelona<br> 
 
    <input type="radio" value="b" name="question3">b) Madrid <br> 
 
    <input type="radio" value="c" name="question3">c) Milan<br> 
 
    <input type="radio" value="d" name="question3">d) Rome<br> 
 
</div> 
 

 
<div class="qheader"> 
 
    <p> 4) Which ocean is the largest?</p> 
 
</div> 
 
<div class="questsel"> 
 
    <input type="radio" value="a" name="question4">a) Arctic<br> 
 
    <input type="radio" value="b" name="question4">b) Pacific<br> 
 
    <input type="radio" value="c" name="question4">c) Indian<br> 
 
    <input type="radio" value="d" name="question4">d) Atlantic<br> 
 
</div> 
 

 
<div class="qheader"> 
 
    <p> 5) Which of these rivers is largest in North America?</p> 
 
</div> 
 
<div class="questsel"> 
 
    <input type="radio" value="a" name="question5">a) Missouri River<br> 
 
    <input type="radio" value="b" name="question5">b) Nile River <br> 
 
    <input type="radio" value="c" name="question5">c) Amazon River<br> 
 
    <input type="radio" value="d" name="question5">d) Yenisei River<br> 
 
</div> 
 

 
<p> 
 
    <input type="button" id="init" value="Submit" /> 
 
</p> 
 

 
<p id="results"></p>

+0

多分 'のvar score'は、このインスタンスのグローバル変数として削除すべきか? –

+0

@barmarありがとうございました。私はちょうど始まっているし、問題と決議のそのすばらしい内訳。それはあなたの詳細な説明のために私の頭の中で物事をクリックします。心から感謝する! –

+0

@AlexNagatkinこれで問題が解決した場合は、その横のチェックマークをクリックして回答を受け入れる必要があります。 – Barmar

関連する問題