2

これは非常に単純なコードで、他の質問と似ています。[送信] をクリックすると、オプション 1 の場合は 3 回、オプション 2 の場合は 2 回、オプション 3 の場合は 1 回、アラート ボックスが表示されます。

問題が発生している可能性が最も高いコード部分は次のとおりです。

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 に答える