JavaScript:

function validateForm(){

  var getNoun = document.formPG.fNoun.value;
  var getVerb = document.formPG.fVerb.value;
  var getPronoun = document.formPG.fPronoun.value;
  var getAdverb = document.formPG.fAdverb.value;
  var getAdjective = document.formPG.fAdjective.value;
  var getSillyWord = document.formPG.fSillyWord.value;
  var getMagic = document.formPG.fMagic.value;

  if((getNoun) === ""){
    alert("You entered a number value, please enter a Noun.");
    document.formPG.fNoun.focus();
    document.getElementById('iNoun').style.borderColor = "red";
    return false;
  }

  //write story to [document].html
paragraph = "There was once a " + getAdjective + " magician who roamed the wild terrains of " + getAdverb + ".<br>";
paragraph += "The magician " + getNoun + " cast the mighty spell " + getMagic + " around the " + getSillyWord + ".<br>" + getPronoun + " knew there was only one way to win the war - " + getVerb + ".";


document.write(paragraph);
}


HTML:

<body>
  <div class="container">
  <h1>Mad Lib</h1>

    <form name="formPG" onsubmit="validateForm()" method="post">
      Noun: <input type="text" name="fNoun" id="iNoun"><br>
      Pronoun: <input type="text" name="fPronoun" id="iPronoun"><br>
      Verb: <input type="text" name="fVerb" id="iVerb"><br>
      Adverb: <input type="text" name="fAdverb" id="iAdverb"><br>
      Adjective: <input type="text" name="fAdjective" id="iAdjective"><br>
      Silly Word: <input type="text" name="fSillyWord" id=""iSillyWord"><br>
      Magic Spell: <input type="text" name="fMagic" id="iMagic"><br>
      <input type="submit" value="submit">
    </form>
    <br>
  <script src="madLib_v2.js"></script>

  </div>
</body>


问题是,每当我点击“提交”按钮时,页面就会点击document.getElementById('iNoun').style.borderColor = "red";并消失。页面会立即刷新,并且该框仅突出显示一秒钟。我希望它停留在该页面上,直到基本刷新页面或直到它们正确为止。

最佳答案

使用return validateForm()。然后仅阻止页面刷新。
删除元素属性中的多余空格和引号,例如id=""iSillyWord"-extra quotestype="submit "-unwanted space




function validateForm() {

  var getNoun = document.formPG.fNoun.value;
  var getVerb = document.formPG.fVerb.value;
  var getPronoun = document.formPG.fPronoun.value;
  var getAdverb = document.formPG.fAdverb.value;
  var getAdjective = document.formPG.fAdjective.value;
  var getSillyWord = document.formPG.fSillyWord.value;
  var getMagic = document.formPG.fMagic.value;

  if ((getNoun) === "") {
    alert("You entered a number value, please enter a Noun.");
    document.formPG.fNoun.focus();
    document.getElementById('iNoun').style.borderColor = "red";
    return false;
  }

  //write story to [document].html
  paragraph = "There was once a " + getAdjective + " magician who roamed the wild terrains of " + getAdverb + ".<br>";
  paragraph += "The magician " + getNoun + " cast the mighty spell " + getMagic + " around the " + getSillyWord + ".<br>" + getPronoun + " knew there was only one way to win the war - " + getVerb + ".";


  document.write(paragraph);

}

<body>
  <div class="container">
    <h1>Mad Lib</h1>

    <form name="formPG" onsubmit="return validateForm()" method="post">
      Noun: <input type="text" name="fNoun" id="iNoun"><br> Pronoun: <input type="text" name="fPronoun" id="iPronoun"><br> Verb: <input type="text" name="fVerb" id="iVerb"><br> Adverb: <input type="text" name="fAdverb" id="iAdverb"><br> Adjective:
      <input type="text" name="fAdjective" id="iAdjective"><br> Silly Word: <input type="text" name="fSillyWord" id="iSillyWord">
      <br> Magic Spell: <input type="text " name="fMagic" id="iMagic"><br>
      <input type="submit" value="submit">
    </form>
    <br>


  </div>
</body>

07-24 09:50
查看更多