P粉5738097272023-08-29 11:58:39
You need to save the selected ID to a hidden field in the form.
<input type="hidden" id="selectedValues" name="selectedValues">
Add onclick() checkbox and add the following function to JavaScript.
function addRemove(id){
// const selectedIds = $('#selectedValues').val();
var selectedIds = JSON.parse($('#selectedValues').val());
console.log(selectedIds);
if($('#' + id).is(":checked")){
//Add if id not there in array
selectedIds.push(id);
}else{
//Remove from the array
selectedIds = selectedIds.filter(function(item) {
return item !== id
})
}
$("#selectedValues").val(JSON.stringify(selectedIds));
console.log(selectedIds)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike" onclick="addRemove('vehicle1')"> Bike
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car" onclick="addRemove('vehicle2')"> Car
<input type="hidden" name="selectedValues" id="selectedValues" value="[]">
P粉9577231242023-08-29 09:42:30
I think you want to add or remove items from the array and it can be used for pagination of the data table. I found a working solution, you can check if it works for you Checkboxes in DataTables need to capture all checked values