如何使用Javascript为警报编码无线电组

时间:2015-03-26 03:37:52

标签: javascript

我目前在我的HTML和一个简单的是或否的广播组中有一个问题。我想这样做,以便当用户回答是,他们得到一个好的工作警报。当他们回答“否”时,警报会再次尝试并重新加载页面。

这是html

    <div id="enterText"></div>

<img src="../img/Window1.jpg" width="500" height="300"><br>

<input type="radio" name="radio-group1" value="Yes">Yes
<input type="radio" name="radio-group1" value="No">No<br><br>
<button id="submitBtn">Submit</button>

这是我当前的javascript

if (document.title === "Level 3"){

    document.getElementById("enterText").innerHTML = "Look at the Picture below, Is it possible for the equation to be true?";

    document.getElementById("submitBtn").addEventListener("click", calcChoices, false);
}



function calcChoices(){
    var radioGroup = document.getElementsByName("radio-group1");
    for (var i = 0; i < radioGroup.length; i++) {
        if (radioGroup[i].checked){
            console.log("You clicked: "+ radioGroup[i].value);
        }
    }
    if(radioGroup[i].value="Yes"){
            alert("NICE!")
        }
        if(radioGroup[i].value="No"){
            alert("try again!")
        }
}

3 个答案:

答案 0 :(得分:1)

为您的javascript尝试以下代码:

document.getElementById("enterText").innerHTML = "Look at the Picture below, Is it possible for the equation to be true?";

document.getElementById("submitBtn").addEventListener("click", calcChoices, false);


function calcChoices(){
  var radioGroup = document.getElementsByName("radio-group1");
  for (var i = 0; i < radioGroup.length; i++) {
    if (radioGroup[i].checked) {
      if(radioGroup[i].value==="Yes"){
              alert("NICE!")
          }
        if(radioGroup[i].value==="No"){
            alert("try again!")
        }
    }
  }
}

答案 1 :(得分:1)

我修改了你的代码,我发现了一些错误。你的第一个if语句没有返回true,所以我添加了一个标题。你最后两个if语句,我加在一起。那两个if语句不在你的for循环中。我修复了一些常见的布尔和分号错误。这是结果化的结果:

if (document.title === "Level 3") {

  document.getElementById("enterText").innerHTML = "Look at the Picture below, Is it possible for the equation to be true?";

  document.getElementById("submitBtn").addEventListener("click", calcChoices, false);
}



function calcChoices() {
  var radioGroup = document.getElementsByName("radio-group1");
  for (var i = 0; i < radioGroup.length; i++) {
    if (radioGroup[i].checked) {
      console.log("You clicked: " + radioGroup[i].value);
      if (radioGroup[i].value === "Yes") {
        alert("NICE!");
        break;
      } else {
        alert("Try Again!");
        break;
      }
    }
  }
}
<head>
  <title>Level 3</title>
</head>

<body>
  <div id="enterText"></div>
  <img src="../img/Window1.jpg" width="500" height="300">
  <br>
  <input type="radio" name="radio-group1" value="Yes">Yes
  <input type="radio" name="radio-group1" value="No">No
  <br>
  <br>
  <button id="submitBtn">Submit</button>
</body>

答案 2 :(得分:0)

如果他们拒绝,此代码将重新加载页面。另外,修复了一个小错误,表示每次都选择YES

if (document.title === "Level 3"){

    document.getElementById("enterText").innerHTML = "Look at the Picture below, Is it possible for the equation to be true?";

    document.getElementById("submitBtn").addEventListener("click", calcChoices, false);
}



 function calcChoices(){
      var radioGroup = document.getElementsByName("radio-group1");
      for (var i = 0; i < radioGroup.length; i++) {
           if (radioGroup[i].checked){
                console.log("You clicked: "+ radioGroup[i].value);
                if(radioGroup[i].value==="Yes"){
                     alert("NICE!");
                }
                if(radioGroup[i].value==="No"){
                     alert("try again!");
                     location.reload(); //Added this line
                }
           }
      }
 }