简体   繁体   English

Javascript:检查已选择哪个单选按钮

[英]Javascript: Check which radiobutton has been selected

I've checked the forums and can't find anything that directly helps or relates to my problem, so here goes. 我已经检查了论坛,找不到任何直接有助于或与我的问题有关的内容,因此请继续。 I'm making a game for school and the player is shown 3 classes, each with a radiobutton next to them. 我正在为学校制作游戏,并且为该玩家显示3个班级,每个班级旁边都有一个单选按钮。 I want it to be that I can click on a radiobutton, then after I've made a final decision (about which class to be), I can click the button at the bottom and then the game will proceed. 我希望我可以单击一个单选按钮,然后在做出最终决定(关于要选择哪个班级)之后,可以单击底部的按钮,然后继续游戏。

<html>

<head>
    <meta charset="utf-8">
    <title>Class Select</title>
</head>
<link rel="icon" type="image/x-icon" href="icon.ico">

<body background="bg2.jpg">

<p align="center"><font size="20" face="verdana" color="white" ><b><u>Select a Class</u></b>    </font></p>
<br><br>

<div align="middle">

<!-- Angel -->
<img src="angel.png" height="110" width="110" alt="The Angel Class">
<input type="radio"  size="20" name="class" class="big" value="Angel" id="class_angel">
<p><font color="aquamarine" size="8"><b>The Angel</b></font></p>
<p><font color="aquamarine" size="6">A born fighter, for all that is good in the world.</font>        </p>
<br>
<p><font color="darkcyan" size="6"><b>Health: 10</b></font></p>
<p><font color="darkcyan" size="6"><b>Damage: 6</b></font></p>
<p><font color="darkcyan" size="6"><b>Starting Item: Halo</b></font></p>
<p><font color="darkcyan" size="4"><b>(Chance of doing double damage when on less than 5 health)

    </b></font></p>
<br><br>

<!-- Beast -->
<img src="beast.png" height="110" width="110" alt="The Beast Class">
<input type="radio"  size="20" name="class" class="big" value="Beast" id="class_beast">
<p><font color="brown" size="8"><b>The Beast</b></font></p>
<p><font color="brown" size="6">Savage at heart, teeth and mind.</font></p>
<br>
<p><font color="burlywood" size="6"><b>Health: 15</b></font></p>
<p><font color="burlywood" size="6"><b>Damage: 4</b></font></p>
<p><font color="burlywood" size="6"><b>Starting Item: Golden Ring</b></font></p>
<p><font color="burlywood" size="4"><b>(Chance of finding more gold)</b></font></p>
<br><br>

<!-- Spirit -->
<img src="spirit.png" height="110" width="110" alt="The Spirit Class">
<input type="radio"  size="20" name="class" class="big" value="Spirit" id="class_spirit">
<p><font color="papayawhip" size="8"><b>The Spirit</b></font></p>
<p><font color="papayawhip" size="6">Easier to see through his lies.</font></p>
<br>
<p><font color="thistle" size="6"><b>Health: 5</b></font></p>
<p><font color="thistle" size="6"><b>Damage: 10</b></font></p>
<p><font color="thistle" size="6"><b>Starting Item: Wandering Soul</b></font></p>
<p><font color="thistle" size="4"><b>(Chance of draining an enemies life)</b></font></p>
<br><br>

<img onclick="continueCheck()" src="next.png" alt="Continue" class="button">

</div>

<style type="text/css" media="screen">
.big{ width: 8em; height: 8em; }
.medium{ width: 16em; height: 8em; }
.button{ width: 6em; height:6em; }
</style>


<!-- Scripting -->
<script type="text/javascript">
function continueCheck()
{
    if(document.getElementById("class_angel") value="Angel")
    {
        console.log("You pressed Angel");
    }else if(document.getElementById("class_beast") value="Beast")
    {
        console.log("You pressed Beast");
    }else if(document.getElementById("class_spirit") value="Spirit")
    {
        console.log("You pressed Spirit");
    }
}
</script>

</body>

</html>

The area at the bottom, in the tag, doesn't seem to work. 标签底部的区域似乎无效。 I get the error "Uncaught ReferenceError: continueCheck is not defined VM68 classSelect.html:52" 我收到错误消息“未捕获的ReferenceError:未定义VM68 classSelect.html:52的continueCheck”

Your syntax is all jacked up: 您的语法全都提高了:

if(document.getElementById("class_angel") value="Angel")

see how you have a random ")" at the end of your line? 看看行尾有一个随机的“)”吗?

edit: I think you probably meant to write: 编辑:我想你可能打算写:

function continueCheck()
{
    if(document.getElementById("class_angel").checked)
    {   
        console.log("You pressed Angel");
    }
    else if(document.getElementById("class_beast").checked)
    {    
        console.log("You pressed Beast");
    }
    else if(document.getElementById("class_spirit").checked)
    {

        console.log("You pressed Spirit");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查在javascript中选择了哪个单选按钮? - How to check which radio button has been selected in javascript? 使javascript检查选择了哪种广播形式 - Making javascript check what radio form has been selected 如何检查是否选择了单选按钮(javascript,html) - How to check if radiobutton is selected (javascript, html) 点击后如何检查单选按钮是否已被检查? - How to check if a radiobutton was already checked, after it has been clicked? 如何使用JavaScript从列表中检查选中了哪些复选框以及量角器中没有的复选框 - How to check from a list which check boxes have been selected and which are not in protractor using javascript JavaScript:需要查找在给定字符串中已选择(突出显示)多个子字符串中的哪个 - JavaScript: Need to find which of multiple substring occurrences has been selected(highlighted) in a given string Javascript:在没有JQuery的情况下检索已选择了哪个下拉菜单? - Javascript: Retrieving which of a drop-down menu has been selected without JQuery? 如何确定一个文件是否已在 JavaScript 中被选中? - How to determining if a file has been selected in JavaScript? 用 jQuery 检查哪个元素被点击 - Check which element has been clicked with jQuery jQuery检查已删除的元素 - Jquery check which element has been dropped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM