简体   繁体   中英

How to restrict page reloading after non-submitting button click on form?

I'm trying to make a button on an HTML form add elements to the form:

 function createRow(){ var row = document.createElement('div'); var input=document.createElement('input'); input.setAttribute("type", "Textbox"); var time=document.createElement('input'); time.setAttribute("type", "Textbox"); row.appendChild(input); row.appendChild(time); return row; } function addRow(){ console.log("z"); var routes = document.getElementById("routes"); routes.appendChild(createRow()); return false; } 
 <form id="routes"> <button onclick="addRow()">+</button> </form> 

But on each button click the page reloads with a question mark added to url. I've found a pair of ideas:

  1. preventDefault(e)
  2. return false

but none worked.

Please tell me what shall I fix? Also there are some conditions:

  1. This is not the submit button, which will be added later
  2. It's required to keep it as simple as possible, the interface is not main focus of project

The default behavior of button is to submit. Specify the type=button and it will override this default behavior.

<button type="button" onclick="addRow()">+</button>

The onclick function should return false . Also addRow() needs to return false :

<form id="routes">
   <button onclick="return addRow()">+</button>
</form>

Ok, there is example of using preventDefault on event listener.

  function createRow(){ var row = document.createElement('div'); var input=document.createElement('input'); var time=document.createElement('input'); input.setAttribute("type", "Textbox"); time.setAttribute("type", "Textbox"); row.appendChild(input); row.appendChild(time); return row; } function addRow(){ document.getElementById("routes").appendChild(createRow()); } document.querySelector("button").addEventListener("click", function(event) { event.preventDefault(); addRow(); }); 
 <form id="routes"> <button>+</button> </form> 

Inside of a form an HTML5 button element has a default type of submit. If you give it a type of "button" everything works fine

<!DOCTYPE html>
<html>
 <head>
 </head>
 <body>
 <form id="routes">
     <div><button type="button" onclick="addRow()">+</button></div>
 </form>
 <script type="text/javascript">
     function createRow(){
         var row = document.createElement('div');
         var input=document.createElement('input');
         input.setAttribute("type", "Textbox");
         var time=document.createElement('input');
         time.setAttribute("type", "Textbox");
         row.appendChild(input);
         row.appendChild(time);
         console.log(row);
         return row;
     }
     function addRow(){
         console.log("z");
         var routes = document.getElementById("routes");
         routes.appendChild(createRow());
         return false;
     }
 </script>
 </body>
 </html>

Give button a id and put preventDefault inside $(document).ready(..) function

  $(document).ready(function(){ $( "#routes_button" ).click(function( event ) { event.preventDefault(); }); }); function createRow(){ var row = document.createElement('div'); var input=document.createElement('input'); input.setAttribute("type", "Textbox"); var time=document.createElement('input'); time.setAttribute("type", "Textbox"); row.appendChild(input); row.appendChild(time); return row; } function addRow(){ console.log("z"); var routes = document.getElementById("routes"); routes.appendChild(createRow()); return false; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <form id="routes"> <button id="routes_button" onclick="addRow()">+</button> </form> 

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