2

这是非常简单的代码,与我的其他问题类似。当我单击提交时,选项一的警告框显示三次,选项二显示两次,三选项显示一次。

以下是最有可能出现问题的代码部分:

var chosen = ""
var len = document.ExamEntry.r1.length

for (var i = 0; i < len; i++) {
  if (document.ExamEntry.r1[i].checked) {
    chosen = document.ExamEntry.r1[i].value
  }
  if (chosen != "") {
    confirm(chosen)
  }
}

这是我的整个代码。除了这个,一切都很好。

<!-- saved from url=(0055)file:///C:/Users/Bartek/Downloads/Exam%20entry4.1.2.htm -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></head><body><h1>Exam Entry Form</h1>
    <form name="ExamEntry" method="post" action="file:///C:/Users/Bartek/Downloads/success.html">
        <input type="radio" name="r1" value="GCSE">GCSE
<input type="radio" name="r1" value="AS">AS
<input type="radio" name="r1" value="A2">A2
<table width="50%" border="0">
            <tbody>
<tr>
                <td id="name" style="color: black; ">Name</td>
                <td>
                    <input type="text" name="name">
                </td>
            </tr>
            <tr>
                <td id="subject" style="color: black; ">Subject</td>
                <td>
                    <input type="text" name="subject">
                </td>
            </tr>
<tr>
                <td id="enumber" style="color: black; ">Examination Number</td>
                <td>
                    <input type="text" name="enumber">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="Submit" value="Submit" onclick=" return validateForm();">
                </td>
                <td>
                    <input type="reset" name="Reset" value="Reset">
                </td>
            </tr>
        </tbody></table>
    </form>

<script>
function validateForm() {
  var result = true;
  var msg = "";
  if (document.ExamEntry.name.value == "") {
    msg += "You must enter your name \n";
    document.ExamEntry.name.focus();
    document.getElementById('name').style.color = "red";
    result = false;
  }
  if (document.ExamEntry.subject.value == "") {
    msg += "You must enter the subject \n";
    document.ExamEntry.subject.focus();
    document.getElementById('subject').style.color = "red";
    result = false;
  }
  if (document.ExamEntry.enumber.value.length != 4) {
    msg += "The examination number must be exactly four characters long \n";
    document.ExamEntry.enumber.focus();
    document.getElementById('enumber').style.color = "red";
    result = false;
  }
  var chosen = ""
  var len = document.ExamEntry.r1.length

  for (var i = 0; i < len; i++) {
    if (document.ExamEntry.r1[i].checked) {
      chosen = document.ExamEntry.r1[i].value
    }
    if (chosen != "") {
      confirm(chosen)
    }
  }
  if (msg == "") {
    return result;
  } {
    alert(msg);
    return result;
  }
} 
</script>
</body>
</html>  

这是 GCSE 计算课程。

4

2 回答 2

1
for (var i = 0; i <len; i++) {
if (document. ExamEntry.r1[i].checked) {
chosen = document. ExamEntry.r1[i].value
}
if (chosen != "") {
confirm(chosen)
}
}

chosen""如果它是以前设置的,则不会;如果未检查该项目,您不会将其设置回"",因此它将confirm是最后一个。只需合并它们。

for(var i = 0; i < document.ExamEntry.r1.length; i++) {
    if(document.ExamEntry.r1[i].checked) {
        confirm(document.ExamEntry.r1[i].value);
    }
}
于 2012-09-09T21:15:28.997 回答
0

你错过了一个else.

if (!msg) {
    return result;
} else {
    alert(msg);
    return result;
}
于 2012-09-09T21:15:38.843 回答