简体   繁体   中英

Inserted value disappeares after pressing submit

I want to create a form in HTML that can take the inserted Name by the user and then after pressing submit shows the corresponded value (Age) to that user. This is my code:

<!DOCTYPE html>
  <html lang="en">
   <head>
       <meta name="viewport" content="width=device-width, initial-scale=1">
       <link rel="stylesheet" type="text/css" href="MyMain.css">
       <script language="JavaScript">
        function myFunction() {
            document.getElementById('myshow').innerHTML = 
                        document.getElementById("Name").value;
          }
       </script>
   </head>
   <body>
      <div class="container">
            <div>
              <fieldset>
                <form action="">
                  <div class="row">
                    <div form-group">
                      <label for="fname">Your Name: </label>
                      <input type="text" class="form-control" name="name" id="Name" placeholder="Jon" value="">
                    </div>
                    </div>
                    <div>
                      <input type="submit" class="button" onclick="myFunction();" value="Submit"<br/>
                    </div>
                  </div>

                </form>

              </fieldset>
            </div>
      </div>
      <div class="container">
      <fieldset>
        <div>
            <label>Age: </label>
            <p><span id='myshow'></span></p>
        </div>
      </fieldset>
     </div>
   </body>
</html>

The problem is that after pressing submit the Name will be shown in the myshow span section(Age:) just for a fraction of second and then it disappeares and url changes to localhost:5000/?Name=Jack rather than localhost:5000/the current path/?Name=Jack

You should use onSubmit on the form element to call the function and then return false.

   <!DOCTYPE html>
  <html lang="en">
   <head>
       <meta name="viewport" content="width=device-width, initial-scale=1">
       <link rel="stylesheet" type="text/css" href="MyMain.css">
       <script language="JavaScript">
        function myFunction() {
            document.getElementById('myshow').innerHTML = 
                        document.getElementById("Name").value;

             return false;
          }
       </script>
   </head>
   <body>
      <div class="container">
            <div>
              <fieldset>
                <form action="" onSubmit="return myFunction();">
                  <div class="row">
                    <div form-group">
                      <label for="fname">Your Name: </label>
                      <input type="text" class="form-control" name="name" id="Name" placeholder="Jon" value="">
                    </div>
                    </div>
                    <div>
                      <input type="submit" class="button" value="Submit"><br/>
                    </div>
                  </div>

                </form>

              </fieldset>
            </div>
      </div>
      <div class="container">
      <fieldset>
        <div>
            <label>Age: </label>
            <p><span id='myshow'></span></p>
        </div>
      </fieldset>
     </div>
   </body>
</html>

Ps: you did not close the submit tag properly

I think that you don't need submit.
If you don't need to submit, You have to change this code.

<!DOCTYPE html>
  <html lang="en">
   <head>
       <meta name="viewport" content="width=device-width, initial-scale=1">
       <link rel="stylesheet" type="text/css" href="MyMain.css">
       <script language="JavaScript">
        function myFunction() {
            document.getElementById('myshow').innerHTML = 
                        document.getElementById("Name").value;
          }
       </script>
   </head>
   <body>
      <div class="container">
            <div>
              <fieldset>
                  <div class="row">
                    <div form-group">
                      <label for="fname">Your Name: </label>
                      <input type="text" class="form-control" name="name" id="Name" placeholder="Jon" value="">
                    </div>
                    </div>
                    <div>
                      <input type="button" class="button" onclick="myFunction();" value="Submit"<br/>
                    </div>
                  </div>
              </fieldset>
            </div>
      </div>
      <div class="container">
      <fieldset>
        <div>
            <label>Age: </label>
            <p><span id='myshow'></span></p>
        </div>
      </fieldset>
     </div>
   </body>
</html>

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