1
I have the following form to record tasks. I record tasks separately.
Code:
<div class="form-group">
<div>
<label for="Indicar">Comeu Bem: </label>
<label for="Notas"> Sim</label>
<input type="checkbox" name="Notas" id="Notas11" Value="Sim" required />
<label for="check1"> Não</label>
<input type="checkbox" name="Notas" id="Notas12" Value="Não" required />
</div>
<input type="hidden" id="tarefa18" name="tarefa" value="30">
<button class="btn btn-info btn-sm botao" onclick="inserir_registo18();if(confirm('Pretende registar esta atividade?')) this.disabled=true;">Pequeno Almoço</button><button type="button" name="abrir18"><span class="glyphicon glyphicon-comment"></span></button>
<div class="form-group" id="comentario18" style="display:none">
<textarea id="Observacao18" name="Observacao" style="color: black;"></textarea>
</div>
</div>
</div>
Ajax to insert:
function inserir_registo18()
{
var dadosajax = {
'tarefa' : $("#tarefa18").val(),
'Notas' : $("#Notas11").val(),
'Notas' : $("#Notas12").val(),
'Observacao' : $("#Observacao18").val()
};
$.ajax({
url: './insertarefa1',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
},
success: function(result)
{
}
});
}
I intend that whenever you perform this action onclick="inserir_registo18();if(confirm('Pretende registar esta atividade?')) this.disabled=true;"
check that one of the checkboxes is filled, if it is not filled in, do not perform the action and force one of the checkboxes to be filled in.
I was trying to:
function inserir_registo18()
{
var checado = $("#Notas11").is(':checked')
var checado1 = $("#Notas12").is(':checked')
if(checado == true || checado1 == true){
var dadosajax = {
'tarefa' : $("#tarefa18").val(),
'Notas' : $("#Notas11").val(),
'Notas' : $("#Notas12").val(),
'Observacao' : $("#Observacao18").val()
};
$.ajax({
url: './insertarefa1',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
},
success: function(result)
{
}
});
}
}
Question edition:
Example:
<div class="form-group">
<div>
<label for="Indicar">Comeu Bem: </label>
<label for="Notas"> Sim</label>
<input type="checkbox" name="Notas" id="Notas11" Value="Sim" required />
<label for="check1"> Não</label>
<input type="checkbox" name="Notas" id="Notas12" Value="Não" required />
</div>
<input type="hidden" id="tarefa18" name="tarefa" value="30">
<button class="btn btn-info btn-sm botao" onclick="inserir_registo18();if(confirm('Pretende registar esta atividade?')) this.disabled=true;">Pequeno Almoço</button><button type="button" name="abrir18"><span class="glyphicon glyphicon-comment"></span></button>
<div class="form-group" id="comentario18" style="display:none">
<textarea id="Observacao18" name="Observacao" style="color: black;"></textarea>
</div>
</div>
<div class="form-group">
<div>
<label for="Indicar">Comeu Bem: </label>
<label for="Notas"> Sim</label>
<input type="checkbox" name="Notas" id="Notas" Value="Sim" required />
<label for="check1"> Não</label>
<input type="checkbox" name="Notas" id="Notas1" Value="Não" required />
</div>
<input type="hidden" id="tarefa19" name="tarefa" value="31">
<button class="btn btn-info btn-sm botao" onclick="inserir_registo19();if(confirm('Pretende registar esta atividade?')) this.disabled=true;">Almoço</button><button type="button" name="abrir19"><span class="glyphicon glyphicon-comment"></span></button>
<div class="form-group" id="comentario19" style="display:none">
<textarea id="Observacao19" name="Observacao" style="color: black;"></textarea>
</div>
</div>
<div class="form-group">
<div>
<label for="Indicar">Comeu Bem: </label>
<label for="Notas"> Sim</label>
<input type="checkbox" name="Notas" id="Notas2" Value="Sim" required />
<label for="check1"> Não</label>
<input type="checkbox" name="Notas" id="Notas3" Value="Não" required />
</div>
<input type="hidden" id="tarefa20" name="tarefa" value="32">
<button class="btn btn-info btn-sm botao" onclick="inserir_registo20();if(confirm('Pretende registar esta atividade?')) this.disabled=true;">Lanche</button><button type="button" name="abrir20"><span class="glyphicon glyphicon-comment"></span></button>
<div class="form-group" id="comentario20" style="display:none">
<textarea id="Observacao20" name="Observacao" style="color: black;"></textarea>
</div>
</div>
javascript:
function inserir_registo18()
{
var checado = $("#Notas11").is(':checked')
var checado1 = $("#Notas12").is(':checked')
var nome = $("#IniciarTarefa").val();
if(nome == ""){
alert("Tem de Iniciar a tarefa!");
}else{
if(checado == true || checado1 == true){
var dadosajax = {
'codigo' : $("#codigo").val(),
'IniciarTarefa' : $("#IniciarTarefa").val(),
'Colaborador' : $("#Colaborador").val(),
'tarefa' : $("#tarefa18").val(),
'Notas11' : $("#Notas11").is(":checked") ? $("#Notas11").val() : '',
'Notas12' : $("#Notas12").is(":checked") ? $("#Notas12").val() : '',
'check' : $("#check").is(":checked") ? $("#check").val() : '',
'Acompnhante' : $("#Acompnhante").val(),
'Observacao' : $("#Observacao18").val()
};
$.ajax({
url: './insertarefa4',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
},
success: function(result)
{
$('#check').prop('checked', false).val("Sim");
$("#Acompnhante").val("").hide();
}
});
}else {
alert('Selecione um campo');
}
}
}
function inserir_registo19()
{
var checado = $("#Notas").is(':checked')
var checado1 = $("#Notas1").is(':checked')
var nome = $("#IniciarTarefa").val();
if(nome == ""){
alert("Tem de Iniciar a tarefa!");
}else{
if(checado == true || checado1 == true){
var dadosajax = {
'codigo' : $("#codigo").val(),
'IniciarTarefa' : $("#IniciarTarefa").val(),
'Colaborador' : $("#Colaborador").val(),
'tarefa' : $("#tarefa19").val(),
'Notas' : $("#Notas").is(":checked") ? $("#Notas").val() : '',
'Notas1' : $("#Notas1").is(":checked") ? $("#Notas1").val() : '',
'check' : $("#check").is(":checked") ? $("#check").val() : '',
'Acompnhante' : $("#Acompnhante").val(),
'Observacao' : $("#Observacao19").val()
};
$.ajax({
url: './insertarefa5',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
},
success: function(result)
{
$('#check').prop('checked', false).val("Sim");
$("#Acompnhante").val("").hide();
}
});
}else {
alert('Selecione um campo');
}
}
}
function inserir_registo20()
{
var checado = $("#Notas2").is(':checked')
var checado1 = $("#Notas3").is(':checked')
var nome = $("#IniciarTarefa").val();
if(nome == ""){
alert("Tem de Iniciar a tarefa!");
}else{
if(checado == true || checado1 == true){
var dadosajax = {
'codigo' : $("#codigo").val(),
'IniciarTarefa' : $("#IniciarTarefa").val(),
'Colaborador' : $("#Colaborador").val(),
'tarefa' : $("#tarefa20").val(),
'Notas2' : $("#Notas2").is(":checked") ? $("#Notas2").val() : '',
'Notas3' : $("#Notas3").is(":checked") ? $("#Notas3").val() : '',
'check' : $("#check").is(":checked") ? $("#check").val() : '',
'Acompnhante' : $("#Acompnhante").val(),
'Observacao' : $("#Observacao20").val()
};
$.ajax({
url: './insertarefa6',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
},
success: function(result)
{
$('#check').prop('checked', false).val("Sim");
$("#Acompnhante").val("").hide();
}
});
}else {
alert('Selecione um campo');
}
}
}
What a mistake you’re making friend?
– Maycon F. Castro
You’re trying with an id that doesn’t exist...
– Darlei Fernando Zillmer
@Maycon F. Castro, is not giving error, and does not register in the database, but the user gets the idea that he has registered in the database, and should not end the process of locking the button until the checkbox is filled
– Bruno
@Darlei Fernando Zillmer already changed the ids, so does not register, but intended that when clicking the button to enter, if it checked that none of the checkboxes were not filled, show an alert to fill and only after filling the action of the insert button
– Bruno
Possible duplicate of Checkbox always returns false when I do . is(":checked");
– Darlei Fernando Zillmer
@Darlei Fernando Zillmer, the question is not duplicated, my problem is different. I will explain: When I do to enter into the database, if the process is completed, disables the button to enter that task, and what I want is that if it turns out that no checkbox is completed, does not finish the button process to the point of disabling it, to insert the task
– Bruno