简体   繁体   中英

onclick event doesn't work after form closing tag

The problem is that onclick event button works fine before form starting tag (at the begining of the html document) but it doesn't work after form closing tag! I want to know why this happens? What is the relation between the place of my button and js function call?

    <form name="Register" action="#" method="post">
      <fieldset>
        <legend>Personal Information</legend>
      <label>Username</label>
      <input type="text" name="username" placeholder="Enter your name">

      <br><br>

      <label>Password</label>
      <input type="password" name="password" placeholder="Enter your password">

      <br><br>
      <label>Message</label>
      <br>
      <textarea name="message" rows="8" cols="50" readonly>Your Message (currently disabled)</textarea>
      <br><br>

      <label>Upload your cv</label>
      <input type="file" name="cv">

      <br><br>

      <label>Remember me</label>
      <input type="checkbox" name="remember">
      </fieldset>

      <br><br>

      <fieldset>
      <legend>Favourits</legend>
      <label>Choose your browser</label>
      <br>
      <input type="radio" name="browser"> Oprera
      <br>
      <input type="radio" name="browser"> Chrome
      <br>
      <input type="radio" name="browser"> Firefox

      <br><br>

      <label>Choose your Mobile</label>
      <br>
      <input type="radio" name="mobile"> Samsung
      <br>
      <input type="radio" name="mobile"> Apple
      <br>
      <input type="radio" name="mobile"> Nokia
      </fieldset>
      <br><br>

      <fieldset>
      <legend>Button Options</legend>
      <input type="hidden" value="testing">
      <input type="submit" name="submit_Register" value="submit">
      <input type="reset" name="" value="reset">
      </fieldset
    </form>

    <input type="button" name="sayHello" value="sayHello" onclick="sayHello()" />

and here's the script

<script>
      function sayHello()
      {
        alert("Hello");
      }
</script>

Your button is not working because of invalid closing </fieldset> tag:

</fieldset

 function sayHello() { alert("Hello"); }
 <input type="button" name="sayHello" value="sayHello" onclick="sayHello()" /> <form name="Register" action="#" method="post"> <fieldset> <legend>Personal Information</legend> <label>Username</label> <input type="text" name="username" placeholder="Enter your name"> <br><br> <label>Password</label> <input type="password" name="password" placeholder="Enter your password"> <br><br> <label>Message</label> <br> <textarea name="message" rows="8" cols="50" readonly>Your Message (currently disabled)</textarea> <br><br> <label>Upload your cv</label> <input type="file" name="cv"> <br><br> <label>Remember me</label> <input type="checkbox" name="remember"> </fieldset> <br><br> <fieldset> <legend>Favourits</legend> <label>Choose your browser</label> <br> <input type="radio" name="browser"> Oprera <br> <input type="radio" name="browser"> Chrome <br> <input type="radio" name="browser"> Firefox <br><br> <label>Choose your Mobile</label> <br> <input type="radio" name="mobile"> Samsung <br> <input type="radio" name="mobile"> Apple <br> <input type="radio" name="mobile"> Nokia </fieldset> <br><br> <fieldset> <legend>Button Options</legend> <input type="hidden" value="testing"> <input type="submit" name="submit_Register" value="submit"> <input type="reset" name="" value="reset"> </fieldset> </form> <input type="button" name="sayHello" value="sayHello" onclick="sayHello()" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM