所以我試圖做一個Buzzfeed風格測驗,但我無法從每個答案選項中檢索值並將它們添加在一起以獲得var'score'。我的腳本有一堆不起作用的函數。我只是想要頭腦風暴。我應該使用數組還是輸入好嗎?我很新,所以如果你看到一些可笑的錯誤,請原諒我。謝謝! :)從單選按鈕檢索值(對於初學者)
`
<!DOCTYPE html>
<html>
<head>
<link href="main.css" rel="stylesheet" >
<script>
var score = 0;
var a1, a2, a3, a4;
function next(){
a1 = document.getElementById("shirt").value;
a2 = document.getElementById("zodiac").value;
a3 = document.getElementById("saturday").value;
a4 = document.getElementById("hunger").value;
score = a1+a2+a3+a4;
if(score<=3) {
return("You should eat a banana.")
}
}
function alertFunction(){
alert(score);
}
function checkRadio(){
if(document.getElementById('name')=="shirt") {
}
}
console.log(score);
</script>
<style>
div {
border: 1px solid black;
background-color: #def2ed;
padding: 20px;
margin-left: auto;
}
</style>
</head>
<body>
<form class = "quiz">
<div id="shirt">
<h2>Question #1: What color shirt are you currently wearing? </h2>
<input onlick="checkRadio(this.value, this.name)" id="sh1" type="radio" name="shirt" value="1"> White<br>
<input onlick="checkRadio(this.value, this.name)" id="sh2" type="radio" name="shirt" value="0"> Yellow<br>
<input onlick="checkRadio(this.value, this.name)" id="sh3" type="radio" name="shirt" value="3"> Red<br>
<input onlick="checkRadio(this.value, this.name)" id="sh4" type="radio" name="shirt" value="5"> Other<br>
</div>
<div id="zodiac">
<h2>Question #2: What is your Zodiac sign? </h2>
<input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="1"> Scorpio<br>
<input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="0"> Capricorn<br>
<input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="3"> Gemini<br>
<input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="1"> Pisces<br>
</div>
<div id="saturday">
<h2>Question #3: What do you see yourself doing on a Saturday? </h2>
<input onlick="checkRadio(this.value, this.name)" id="s1" type="radio" name="saturday" value="3"> Sleeping until noon<br>
<input onlick="checkRadio(this.value, this.name)" id="s2" type="radio" name="saturday" value="1"> Hitting the gym<br>
<input onlick="checkRadio(this.value, this.name)" id="s3" type="radio" name="saturday" value="5"> Shopping <br>
<input onlick="checkRadio(this.value, this.name)" id="s4" type="radio" name="saturday" value="1"> Hanging out with friends<br>
</div>
<div id="hunger">
<h2>Question #4: How hungry are you? </h2>
<input onlick="checkRadio(this.value, this.name)" id="h1" type="radio" name="hunger" value="0"> Not really hungry at the moment<br>
<input onlick="checkRadio(this.value, this.name)" id="h2" type="radio" name="hunger" value="1"> I'm a little peckish<br>
<input onlick="checkRadio(this.value, this.name)" id="h3" type="radio" name="hunger" value="0"> I'm pretty hungry, now that you mention it<br>
<input onlick="checkRadio(this.value, this.name)" id="h4" type="radio" name="hunger" value="1"> I'm STARVING!<br>
</div>
<button type="button" class="primaryBtn" onClick="alert(score)">Next</button>
</form>
</body>
</html>`
爲什麼不使用jQuery庫?如果你想用''讓我們知道 - 我會盡我所能來幫助您 –
可能的重複[如何知道哪個單選按鈕是通過jQuery選擇的?](http://stackoverflow.com /問題/ 596351 /我怎麼能知道哪個單選按鈕被選中通過jquery) –