2013-01-08 41 views
0

即將推出的JavaScript,所以請好。 我的網站上有一個類型的quizz。我需要循環選中的單選按鈕並在用戶單擊提交時添加值。通過單選按鈕循環並加起來總計javascript

將值加起來後,必須出現一個小彈出窗口,顯示結果。如果單選按鈕的總價值是0-10顯示味精1 否則之間,如果單選按鈕的總價值爲11-20 diplay味精2 其他顯示味精3

我已經在網上搜索,但我只是不能找到一個之間如何遍歷單選按鈕,並添加了總計簡單的工作說明

我的HTML形式如下

你幾歲?
18-21
22-30
30-42

你怎麼看怎麼樣?
我類似於泰國
我是一個sterotypical外國人(白皮膚,金黃色的頭髮,藍色的眼睛)

我老了一個女孩

你是什麼資格 的水平
高中
大專文憑/證書
大學
碩士或更高

    <strong>From which country are you?</strong><br /> 
       <input type="radio" value="3" name="country" />Aus, NZ, SA, UK, US<br /> 
       <input type="radio" value="0" name="country" />Other country from the once listed above<br /> 
       <br /> 

        <strong>Is English your naitive language</strong><br /> 
       <input type="radio" value="2" name="country" />yes<br /> 
       <input type="radio" value="0" name="country" />no<br /> 
       <input type="radio" value="1" name="country" />no, but I am fluent<br /> 
       <br /> 

       <strong>Do you have any experience in teaching English in Thailand?</strong> 
       <br /> 
       <input type="radio" value="0" name="experience" />none<br /> 
       <input type="radio" value="1" name="experience" />0-1 year<br /> 
       <input type="radio" value="3" name="experience" />1-2 years<br /> 
       <input type="radio" value="4" name="experience" />2 years +<br /> 
       <br /> 

        <strong>What is your knowledge of Thai language and Thai culture</strong> 
       <br /> 
       <input type="radio" value="1" name="culture" />I know nothing<br /> 
       <input type="radio" value="2" name="culture" />I know my basics<br /> 
       <input type="radio" value="3" name="culture" />I know slightly more than the basics<br /> 
       <input type="radio" value="4" name="culture                                                                                                                                                        \ " />My friends say I am Thai<br /> 
       <br /> 

        <strong>What is your knowledge of Thai language and Thai culture</strong> 
       <br /> 
       <input type="radio" value="1" />I know nothing<br /> 
       <input type="radio" value="2" />I know my basics<br /> 
       <input type="radio" value="3" />I know slightly more than the basics<br /> 
       <input type="radio" value="4" />My friends say I am Thai<br /> 
       <br /> 

        <strong>Do you have any visible tatoos or piercings?</strong> 
       <br /> 
       <input type="radio" value="2" />Yes but it is not visible<br /> 
       <input type="radio" value="4" />none<br /> 
       <input type="radio" value="0" />Visible tatoo or piercing<br /> 
       <br /> 

       <strong>What gender are you</strong> 
       <br /> 
       <input type="radio" value="1" />Male<br /> 
       <input type="radio" value="4" />Female<br /> 
       <br /> 
       <input type="submit" onclick="showTotal" /> 


       </form> 
+0

第二個問題似乎缺少一些選項。 –

回答

-1

只需添加下面的腳本在你的腦袋的部分,然後你就會如下得到總價值。

<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script> 
<script type="text/javascript"> 
function showTotal() 
{ 
    total = 0; 
    $("input[type=radio]:checked").each(function(){ 
     total += Number($(this).val()); 
    }); 
    alert(total); 
} 
</script> 
1

這裏這篇文章將向您展示如何得到一個單選按鈕黨團的價值:How can we access the value of a radio button using the DOM?

你最簡單的回答是:

function getRadioValue (theRadioGroup) 
{ 
    for (var i = 0; i < document.getElementsByName(theRadioGroup).length; i++) 
    { 
     if (document.getElementsByName(theRadioGroup)[i].checked) 
     { 
       return document.getElementsByName(theRadioGroup)[i].value; 
     } 
    } 
} 

var country = parseInt(getRadioValue ('country')); 
var experience = parseInt(getRadioValue ('experience')); 

等你需要添加他們都起來,並提醒他們所有的

var total = country + experience; // add all the other variables here 
alert(total); 

希望能幫助。