I have a datatable of salaries with checkboxes, and a select to fill with chantiers, and a button updateAll, I want when I check the lines of salaries and choose a chantier in select and click on button updateAll, chantier of salaries check modify by value select in select. NB: without changing the value of select, ie without touching <option value="1">azilal</option> not <option value="azilal">azilal</option>
in my example it changes azilal by 2 but I want to change azilal by tizgui, it's just an example
datatable
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<select class="form-control" id="chantier">
<option selected disabled>Select Location</option>
<option value="1">azilal</option>
<option value="2">asfalou</option>
<option value="3">tizgui</option>
</select>
<button class="btn btn-theme update-all" data-url="">Update All</button>
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>nom prenom</th>
<th>cin</th>
<th>matricule</th>
<th>chantier</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td><input type="checkbox" class="checkbox" name="customer_id[]" value="1" /></td>
<td>MARZOUK NAJIB</td>
<td>Pa130191</td>
<td>2925019599</td>
<td value="1">azilal</td>
</tr>
<tr id="2">
<td><input type="checkbox" class="checkbox" name="customer_id[]" value="2" /></td>
<td>achraf bourki</td>
<td>pa5000</td>
<td>202020</td>
<td value="2">tizgui</td>
</tr>
</tbody>
</table>
jQuery
<script type="text/javascript">
$(document).ready(function () {
$('#check_all').on('click', function(e) {
if($(this).is(':checked',true))
{
$(".checkbox").prop('checked', true);
} else {
$(".checkbox").prop('checked',false);
}
});
$('.checkbox').on('click',function(){
if($('.checkbox:checked').length == $('.checkbox').length){
$('#check_all').prop('checked',true);
}else{
$('#check_all').prop('checked',false);
}
});
$('.update-all').on('click', function(e) {
if(confirm("Are you sure you want to update this?"))
{
var id = [];
var chantier;
$(':checkbox:checked').each(function(i){
id[i] = $(this).val();
});
if(id.length === 0){
alert("Please Select atleast one checkbox");
}else{
let chantier = $('#chantier').val();
for(var i=0; i<id.length; i++)
{
$('tr#'+id[i]).find('td:last-child').html(chantier);
}
}}
else{
return false;
}
});
});
</script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
Inside javascript, you are selecting value of selected option of #chantier instead of it's text.
All you need to do is change this line as below:
Old line:
let chantier = $('#chantier').val();
Replace with below:
let chantier = $('#chantier option:selected').html();
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.