使用按钮引用单选按钮选项

时间:2015-03-03 04:05:58

标签: javascript jquery html

这是我的HTML。我很肯定HTML没有错误。

我的JavaScript是用psydocode编写的,因为我遇到了大量问题并且语法正确。

如果玩家没有死,每次点击按钮时,3000英里需要是一个明显改变的值。

我需要弄清楚将播放按钮链接到单选按钮选项的语法,检查用户是否随机死亡 - 如果不是 - 从需要旅行的总里程减去500英里(3000),然后检查英里计数器是否为0。

感谢您的时间!

    <body>
    <h1>Coal Train the Game!<h1>
    <h3> Pick one track. Be careful to read the track descriptions,
    some tracks have a better chance to reach Oregon than others!  
    After strategically choosing a track, click the button to travel 500 miles.
    At the end of 3000 miles, you win the game!<h3>


    <img src="coaltrain.jpg" alt="Picture of Train" style="width:600px;height:300">


    <table>
    <tr>
    <td valign="top"><input type="radio" name="You Changed Tracks!" id="Track_1" value="1" onclick="alert(this.name);" /></td>
    <td valign="top" style="color: red;">Scenic Bridge Track - Choose this track if you enjoy old bridges!</td>
</tr>
<tr>
    <td valign="top"><input type="radio" name="You Changed Tracks!" id="Track_2" value="2" checked="checked" onclick="alert(this.name);" /></td>
    <td valign="top" style="color: blue;">Outlaw Town Track - Choose this track if you enjoy talking to unrefined characters!</td>
</tr>
<tr>
    <td valign="top"><input type="radio" name="You Changed Tracks!" id="Track_3" value="3" onclick="alert(this.name);" /></td>
    <td valign="top" style="color: green;">Active Volcano Track - Choose this track if you enjoy liquid hot magma!</td>
</tr>
<tr>
    <td valign="top"><input type="radio" name="You Changed Tracks!" id="Track_4" value="4" onclick="alert(this.name);" /></td>
    <td valign="top" style="color: orange;">Boring Track - Choose this track if you prefer safety over experience</td>
</tr>
</table>


<div id="Miles" value = "3000"> 3000 Miles to Travel!</div>

<button id="Play" onclick="myFunction()">Play</button>

</html> 

function myFunction = If Track_1 = "checked" 

then

Math.Random()<=.25 then alert(The Coal Train died by falling through a bridge!) 

else
    var Miles 
    var OneTurn
    var Total = "Miles"-"One Turn"

if var Miles = 0 

then alert(Your Coal Train made it too Oregon!  Congrats!);

1 个答案:

答案 0 :(得分:0)

function myFunction = If Track_1 = "checked" 

then

Math.Random()<=.25 then alert(The Coal Train died by falling through a bridge!) 

else
    var Miles 
    var OneTurn
    var Total = "Miles"-"One Turn"

if var Miles = 0 

then alert(Your Coal Train made it too Oregon!  Congrats!);

  

以下是Js代码的语法

function myFunction() {
    if(document.getElementById('Track_1').checked) {
        if(Math.random()<=.25) {
          alert("The Coal Train died by falling through a bridge!");
        }
        else {
          var Miles;
          var OneTurn;
          var Total = Miles-OneTurn;
          if(Miles==0) {
            alert("Your Coal Train made it too Oregon! Congrats!);
          }
       }
   }
}
  

你调用函数的方式是正确的。因此,当点击按钮时,代码会检查&#39; #Track_1 &#34;输入(收音机盒)......

     

虽然我写了代码;我基于&#34;缩进&#34;构建了if-else梯形图层次结构。在 psydocode

中提供