Well I see several things in your code:
Since you have a table I guess you have several checkbox
and I see in your code that you select them by means of an id.
Remember: When there are several items that are going to share the same name on the site you should use class
and not id
. The id
is a unique and unrepeatable identifier and it is for this reason that your code only works correctly with the first checkbox
because when you have several elements with the same id
the system will always interact with the first one you find ignoring the rest like that.
If you are using jQuery, why do you use a native JavaScript selector? Just use jQuery from beginning to end.
To check or remove the checked from a checkbox you use the prop()
method of jQuery.
Functional example:
NOTE: I added a setTimeout()
with a small amount of time so you can see how it is checked and how the checked%% is removed%.
$('#formDatosVacantes').on('change','input[type="checkbox"]',function(){
setTimeout(function(){
$(".idVacante").prop('checked', false);
$('#existeEnVacante').modal('show');
}, 200);
});
<!-- CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<table id="formDatosVacantes" class="table">
<thead>
<tr>
<th>Nombre</th>
<th>Check</th>
</tr>
</thead>
<tbody>
<tr>
<td>Usuario 1</td>
<td><input type="checkbox" name="" class="idVacante"></td>
</tr>
<tr>
<td>usuario 2</td>
<td><input type="checkbox" name="" class="idVacante"></td>
</tr>
<tr>
<td>Usuario 3</td>
<td><input type="checkbox" name="" class="idVacante"></td>
</tr>
<tr>
<td>Usuario 4</td>
<td><input type="checkbox" name="" class="idVacante"></td>
</tr>
</tbody>
</table>
<div class="modal fade" id="existeEnVacante" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
And that's how your code should look (remember to change the checkbox
of id
by checkbox
)
$('#formDatosVacantes').on('change','input[type="checkbox"]',function(){
var buttons = "";
var table = $('#tabla').DataTable();
buttons = table.buttons( ['.asociar'] );
var idVacantes = $(this).val();
var idCanPros = <?= json_encode($idCP,JSON_HEX_QUOT |
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS
) ?>;
alert(idCanPros);
$.ajax({
url: ajaxVerificaVacante,
type: 'post',
data: {
idVacantes : idVacantes,
idCanPros : idCanPros,
_csrf : _csrfToken
},
success: function(data){
console.log(data);
if(data.mensajes != 0){
$("#texto").html(data.mensajes);
$(".idVacante").prop('checked', false);
buttons.disable();
$('#existeEnVacante').modal('show');
}
},
error: function(exception){
console.log('error' + exception);
}
});
if(this.checked==true){
buttons.enable();
}else{
buttons.disable();
}
});