0

Please refer to images

  1. I am trying to code a "+" button that when you click on it, my Jquery dynamically adds another size input field image1

  2. The problem is that when it is inside the "form" tag and I click on the button, it appears for 1 second and disappears automatically. When I remove the "form" tag, it works as expected and does not disappear. image2

Here is my the part of my code for the size input causing the problem:

<form method="POST" action="addProduct.php">
   <div class="col-lg-4 topmargin">
   <span><h4>Size</h4></span>
   <table class="table borderless sizeTable" id="dynamic_price" >
      <tr>
        <td> <input class="form-control "type="text" name="size[]" placeholder="200 ml" size="100%"> </td> 
        <td><button  class="btn btn-primary" name="addSize" id="addSize">+</button></td>
      </tr>
  </table>
</div>
</form>

Here is JQuery:

   <script> 
    
    $("#addSize").click(function(){
        $('.sizeTable tbody tr:last').after('<tr><td> <input class="form-control "type="text" name="size[]"  placeholder="9.99" size="100%"> </td><td><button  class="btn btn-danger" name="removeSize">-</button></td></tr>');
    });
    </script>

How can I make it work inside my form?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
King A
  • 37
  • 10

1 Answers1

1

Your button needs to have type="button" otherwise its default is type="submit". If you want to keep the type="submit" (for now), you'll need to use preventDefault() on your event listener to prevent it from doing the default 'submit' action on the form.

kvncnls
  • 261
  • 2
  • 9