2013-10-16 53 views
0

我試圖將所選單選按鈕值與預定值進行比較,然後顯示對應於匹配或不匹配的文本。這似乎應該很簡單,但它顯示「匹配」文本,無論這兩個值是否匹配。將單選按鈕值與預定值進行比較

這裏是我的代碼:

submit: function() { 
    $("#submit").click(function(){ 
     if($("input:radio[name=answers]:checked").val() == triviaGame.correctSelection) { 
      $("#result").text("Correct!"); 
     } else {$("#result").text("I'm sorry, you did not select the correct answer :(");} 
    }); 

我也試過:

submit: function() { 
$("#submit").click(function() { 
    if ($("input:radio[name=answers]:checked").val() == triviaGame.questions[questionSelectNum].choices[triviaGame.questions[questionSelectNum].choices[0]]) { 
     $("#result").text("Correct!"); 
    } else {$("#result").text("I'm sorry, you did not select the correct answer :(");} 
}); 

我已經試過=====以及其他語法調整。

我檢查以確保我在我的nextQuestion方法中正確設置單選按鈕的值,並且我的測試工作,所以我不認爲這是問題所在。最讓我感到困惑的是if(true)代碼執行的時候,它不應該代替else(false)代碼執行,否則我認爲這會是一個更可能的錯誤。

這裏是我的HTML:

<div> 
    <button id = "nextQuestion">Next Question</button> 
    <br></br> 
    <div id = "questionDisplay"></div> 
    <br></br> 
    <input type="radio" id= "1" name="answers" value="defaultVal"> 
    <label id= "labelOne">Answer 1</label> 
    <br></br> 
    <input type="radio" id= "2" name="answers" value="defaultVal"> 
    <label id= "labelTwo">Answer 2</label> 
    <br></br> 
    <input type="radio" id= "3" name="answers" value="defaultVal"> 
    <label id= "labelThree">Answer 3</label> 
    <br></br> 
    <input type="radio" id= "4" name="answers" value="defaultVal"> 
    <label id= "labelFour">Answer 4</label> 
    <br></br> 
    <button id = "submit">Submit</button> 
    <br></br> 
    <div id= "result">Please select an answer above</div> 
</div> 

Here's在jsbin的鏈接全部代碼。

回答

0

道歉,我得到了堅持錯誤的結束首先,我認爲你只需要更新本節:

//sets the values of each radio button to the same value as each radio's corresponding answer 
    $("#1") .val(triviaGame.firstAnswer); 
    $("#2") .val(triviaGame.firstAnswer); 
    $("#3") .val(triviaGame.firstAnswer); 
    $("#4") .val(triviaGame.firstAnswer); 

有了這個:

//sets the values of each radio button to the same value as each radio's corresponding answer 
    $("#1") .val(triviaGame.firstAnswer); 
    $("#2") .val(triviaGame.secondAnswer); 
    $("#3") .val(triviaGame.thirdAnswer); 
    $("#4") .val(triviaGame.fourthAnswer); 

http://jsbin.com/OKecora/1/edit

+0

是,而已。謝謝。 – CoolestUsername

+0

羅傑。再次感謝! – CoolestUsername

+0

不用擔心,很高興幫助:) –