I am new to jquery. I am working in Laravel 5.3. I am loading jquery and datatables min.css and min.js through my master blade template. I am consuming an API to get a list of livestock that belongs to a member and outputting with jquery datatables. Each row is selectable so the user can select which animals they want to enter into a show. The datatable is paginated and I can select animals on each page. However, when I click Enter Show, all animals on the currently displayed page get submitted rather than only those selected, regardless of page, however, the number of animals selected is displayed correctly in the alert. What can I do to fix this so only the selected rows are submitted?
My code:
<form action="/entershowoptions" method="POST" id="entriesSelected">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="show_id" value="{{ $show_id }}">
<input type="hidden" name="memberId" value="{{ $memberId }}">
<table id="goatTable" class="display" cellspacing="0" width="100%">
<thead>
<th>Reg #</th>
<th>Name</th>
<th>DOB</th>
<th>Sex</th>
</thead>
<tbody>
@foreach($mygoatsarray as $mygoat)
@if($buckDivisionCount < 1 && $mygoat->GOAT_SEX == 'BUCK')
@continue
@elseif($doeDivisionCount < 1 && $mygoat->GOAT_SEX == 'DOE')
@continue
@else
<tr>
<td>{{ $mygoat->OLD_REG_NUM }}
<input type="hidden" name="entry_reg_num[]" value="{{ $mygoat->OLD_REG_NUM }}"></td>
<td>{{ $mygoat->GOAT_NAME }}
<input type="hidden" name="entry_reg_name[]" value="{{ $mygoat->GOAT_NAME }}"></td>
<td>{{ $mygoat->DATE_OF_BIRTH }}
<input type="hidden" name="entry_dob[]" value="{{ $mygoat->DATE_OF_BIRTH }}"></td>
<td>{{ $mygoat->GOAT_SEX }}
<input type="hidden" name="entry_sex[]" value="{{ $mygoat->GOAT_SEX }}"></td>
</tr>
@endif
@endforeach
</tbody>
</table>
<br><br>
<button id="submit">Enter Goats</button>
<br><br>
</form>
</center>
<br><br>
</div>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#goatTable').DataTable();
$('#goatTable tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
} );
$('#submit').click( function () {
alert( table.rows('.selected').data().length +' row(s) selected' );
$.ajax({
type: "POST",
url: "/entershowoptions",
data: {'form': $("#entriesSelected").table.rows('.selected').serialize()}
});
});
} );
</script>