2

こんにちは、テキストボックスを検証するためにこのコードを取得しようとしているので、値が含まれている必要があります。そうでない場合は、フィールドを空白にできないことを説明するメッセージを表示したくありません。ここに私が書いたコードがあります:

<head>
<title>Exam entry</title>
<script language="javascript" type="text/javascript">

function validateForm() {
    var result = true;
    var msg="";

        if (document.ExamEntry.name.value=="") {
        msg+="You must enter your name \n";
        document.ExamEntry.name.focus();
        document.gotElementById('name').style.color="red";
        result = false;
    }

        if (document.ExamEntry.subject.value=="") {
        msg+="You must enter the subject \n";
        document.ExamEntry.subject.focus();
        document.gotElementById('subject').style.color="red";
        result = false;
    }

        if (document.ExamEntry.examno.value=="") {
        msg+="You must enter an examination number \n";
        document.ExamEntry.examno.focus();
        document.gotElementById('examno').style.color="red";
        result = false;

    if(msg==""){
    return result;
    }

    {
    alert(msg)
    return result;
    }
}

</script>
</head>

<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
    <tr>
        <td id="name">Name</td>
        <td><input type="text" name="name" /></td>
    </tr>
    <tr>
        <td id="subject">Subject</td>
        <td><input type="text" name="subject" /></td>
    </tr>
    <tr>
        <td id="examno">Examination Number</td>
        <td><input type="integer" name="examno" /></td>
    </tr>

<tr>
    <td><input type="submit" name="Submit" value="Submit" onclick="return validationFrom();" /></td>
    <td><input type="reset" name="Reset" value="Reset" /></td>
</tr>

</table>
</form>
</body>

どんな助けでもありがとう

4

3 に答える 3

2
  1. getElementById と not gotelementById
  2. 予約語 name の代わりに fullName を使用する
  3. フォーム名と同じ ID を使用しないでください - IE が混乱します
  4. 送信ボタンの onclick を使用しないでください。代わりに form onsubmit を使用してください
  5. HTMLファイルへのPOSTは許可されない可能性があります
  6. HTML5 は「integer」ではなく type="number" をサポートします
  7. value各フィールドに属性を追加します

これが私の提案です - DEMO

ラベル ID を変更し、名前を fullName に変更しました

function setError(fld,id,msg) {

   if (fld.value=="") {
     document.getElementById(id).className="error";
     return msg;
   }
   document.getElementById(id).className="normal";
   return "";

}
window.onload=function() {
  document.getElementById("ExamEntry").onsubmit=function() {

    var msg,msgs=[],focusField="";
    msg=setError(this.fullName,"fullNameLabel","You must enter your name");
    if (msg) { msgs.push(msg); focusField = this.fullName;}
    msg=setError(this.subject,"subjectLabel","You must enter the subject");
    if (msg) {  msgs.push(msg); focusField = focusField || this.subject;}

    var examno = this.examno;
    msg = setError(examno,"examnoLabel","You must enter an examination number");
    if (msg) { msgs.push(msg); focusField = focusField || examno;}
    else {
      if (isNaN( examno.value) ||  examno.value.length<4) {
        msgs.push("You must enter a valid examination number");
        document.getElementById("examnoLabel").className="error"
        focusField = focusField || this.examno;
      }
    }
    if(focusField) {
      alert(msgs.join("\n"));
      focusField.focus();
      return false;
    }
    return true; // allow submission
  }
}
于 2013-01-17T10:34:36.150 に答える
1

のような予約語を使用して入力要素に名前を付けると、問題が発生しますname。コード:

document.ExamEntry.name.value

ExamEntryフォームには既に というプロパティがあるため、問題が発生しますname。フィールドの名前を変更するか、次を使用する必要があります。

document.ExamEntry.elements['name'].value
于 2013-01-17T10:35:36.477 に答える
0

属性値は要素で直接利用できないため、常に次を使用する必要があります。

element.getAttribute('value')

これにより、value 属性が取得されます。

お役に立てれば。

于 2013-01-17T10:29:54.243 に答える