2012-04-02 61 views
1

我的分數增加不起作用。兩個問題。得分不遞增javascript

它會自動更新嗎?

或者是我的代碼錯了?

HTML頁面

<script>document.write("Score: " + score);</script> 

<table class="widthOneHundredPercent"> 
<tr> 
<td class="tableCellFloat columnOne" valign="top"> 
    <div class="question"><b>Question 1:</b> Who is the youngest in B Media?</div> 
<td class="tableCellFloat columnTwo" valign="top"> 
    <div class="answers"> 
      <ol> 
       <li> 
        <input type="radio" name="q1" id="q1-a" onclick="unhide(this); scoreIncrement()"/>John 
       </li> 
       <li> 
        <input type="radio" name="q1" id="q1-b" onclick="unhide(this)"/>Paula 
       </li> 
       <li> 
        <input type="radio" name="q1" id="q1-c" onclick="unhide(this)"/>Henal 
       </li> 
       <li> 
        <input type="radio" name="q1" id="q1-d" onclick="unhide(this)"/>Malc 
       </li> 
      </ol> 
     </div> 
</td> 
</td> 
</tr> 
<tr> 
<td colspan="2" valign="top"> 
    <div id="answerq1a" class="hidden"> 
<p>Your answer is correct, John is 20.</p> 
    </div> 
</td> 
</tr> 
<tr> 
<td colspan="2" valign="top"> 
    <div id="answerq1b" class="hidden"> 
<p>Your answer is incorrect, Paula is 31.</p> 
    </div> 
</td> 
</tr> 
<tr> 
<td colspan="2" valign="top"> 
    <div id="answerq1c" class="hidden"> 
<p>Your answer is incorrect, Henal is 21.</p> 
    </div> 
</td> 
</tr> 
<tr> 
<td colspan="2" valign="top"> 
    <div id="answerq1d" class="hidden"> 
<p>Your answer is incorrect, Malc is 33.</p> 
    </div> 
</td> 
</tr> 
</table>​ 

的Javascript

var currentShown = ""; 

function unhide(rad) { 
    var id = "answer" + rad.id.replace("-", ""); 
    var answer = document.getElementById(id); 
    if (answer) { 
     var current = document.getElementById(currentShown); 
     if (current) current.className = "hidden"; 
     currentShown = id; 
     answer.className = "unhidden"; 
    } 
} 

var score = 0; 

function scoreIncrement() { 

    score++; 
}​ 

這種簡單的代碼,但它不會工作,很明顯,我已經寫了錯,但我看不出有什麼不好?

+3

呃,什麼分數增加......在你發佈的代碼中沒有這樣的東西 – scibuff 2012-04-02 16:20:45

+1

哈哈,老實說當我分享時它就在那裏,讓我改變分享鏈接,不知道那裏發生了什麼, 。 – 2012-04-02 16:22:17

+2

http://jsfiddle.net/RfkCw/3/ - 不知道你必須點擊更新,我的壞。新的jsfiddle :) – 2012-04-02 16:23:02

回答

4

你出document.write'ing比分,但從來沒有更新調用,這樣即使你的分數增加它沒有被網頁上更新中...

更新的東西,如您的HTML:

<!-- <script>document.write("Score: " + score);</script> --> 
<span id="score"></span> 

,並與一些你的JavaScript像

function scoreIncrement() { 
    score++; 
    document.getElementById("score").innerText="Score: " + score; 
}​ 

應該這樣做。最優雅的解決方案,也許不是,但它確實有效。

+2

你也打敗了我,所以這裏是一個JS小提琴來讚揚你的答案:http://jsfiddle.net/RfkCw/8/。 – 2012-04-02 16:33:08

+0

謝謝你倆。具有很大的意義,現在讓它防禦,所以沒有作弊,謝謝大家! – 2012-04-02 16:34:51