I'm setting up an input form project for my work, and for certain tables the user needs to be able to insert multiple data rows before moving onto the next form. I have the form set up in a table with input groups in the table cells like this:
$("#addRow").submit(function() {
$("#tableData").each(function() {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function() {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action='/costs_hours' method="POST">
<h1 class="text-center mb-3">Costs and Hours</h1>
<div class="card border-secondary w-100 text-light" style="background-color: #333f48">
<div class="card-body w-100 text-end">
<table id="tableData" class="table text-light text-center mt-3">
<thead>
<tr>
<th scope="col">Project ID</th>
<th scope="col">Implementation or Annual</th>
<th scope="col">Category</th>
<th scope="col">Costs</th>
<th scope="col">Hours</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="input-group mb-3">
<input name="project_id" type="text" class="form-control">
</div>
</td>
<td>
<div class="input-group mb-3">
<div class="input-group mb-3">
<select name="imp_or_ann" class="form-select" id="inputGroupSelect01">
<option disabled selected>Choose...</option>
<option>Implementation</option>
<option>Annual</option>
</select>
</div>
</div>
</td>
<td>
<div class="input-group mb-3">
<div class="input-group mb-3">
<select name="category" class="form-select" id="inputGroupSelect01">
<option disabled selected>Choose...</option>
<option>EMO</option>
<option>Analysts</option>
<option>Maintenance</option>
<option>ETS</option>
<option>BOT</option>
<option>OtherUT</option>
<option>Materials</option>
<option>Non-UT Contract</option>
<option>Contingency</option>
</select>
</div>
</div>
</td>
<td>
<div class="input-group mb-3">
<input name="cost" type="text" class="form-control">
</div>
</td>
<td>
<div class="input-group mb-3">
<input name="hours" type="text" class="form-control">
</div>
</td>
</tr>
</tbody>
<button id='addRow' type="button" style="background-color: #bf5700;" class="btn btn-warning text-light">Add</button>
</table>
<div id="next">
</div>
</div>
</div>
</form>
Because the function calls .submit(), it submits the form and refreshes the page so the $(#tableData) function doesn't work. I tried changing it to a .click() instead of .submit() and also adding e.preventDefault() but both of those methods don't submit the form. Any ideas on how I can achieve this would be greatly appreciate! Thanks!