Jquery ischecked does not work [closed]

3

I have the following checkbox:

<input type="checkbox" name="responderam" id="responderam" value="1" onchange="Filtra()"  /> Mostrar apenas quem respondeu

And the function it calls will refresh the page by setting a flag

function Filtra() {     

    var respondeu = "";
    var valor = $("#responderam").val();

    if($("#responderam").is(':checked')){
        respondeu = 1;
    }
    else {
        respondeu = 0;
    }
}

But this IF does not work at all. And the function also does not take the value "1" of the checkbox even if I check it. I'm using Jquery 1.8.

What am I doing wrong? I've seen answers in other questions but it does not work.

    
asked by anonymous 02.08.2017 / 14:25

3 answers

4

It is working normally, it follows example displaying alert according to the status of ckeckbox and with jQuery 1.8:

function Filtra() {     
    var respondeu = "";
    var valor = $("#responderam").val();

    if($("#responderam").is(':checked')){
        respondeu = 1;
        alert (respondeu);
    }
    else {
        respondeu = 0;
        alert (respondeu);
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><inputtype="checkbox" name="responderam" id="responderam" value="1" onchange="Filtra()"  /> Mostrar apenas quem respondeu

2nd alternative - calling the function by type of input on your page does not exist other checkbox :

$(document).ready(function() {
  $(':checkbox').change(function() {   
    var respondeu = "";
    if($(this).is(':checked')){
        respondeu = 1;
        alert (respondeu);
    }
    else {
        respondeu = 0;
        alert (respondeu);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><inputtype="checkbox" name="responderam" id="responderam" value="1"/> Mostrar apenas quem respondeu

3rd alternative - keeping id of checkbox :

$(document).ready(function() {
  $('#responderam').change(function() {   
    var respondeu = "";
    if($(this).is(':checked')){
        respondeu = 1;
        alert (respondeu);
    }
    else {
        respondeu = 0;
        alert (respondeu);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><inputtype="checkbox" name="responderam" id="responderam" value="1"/> Mostrar apenas quem respondeu
    
02.08.2017 / 14:30
3

function Filtra(evt) {  
  var respondeu = "";
  var valor =  $(evt).val();
    if($(evt).is(':checked')){
        respondeu = 1;
        $(evt).val(1);
        alert (respondeu);
    }
    else {
        respondeu = 0;
        $(evt).val(0);
        alert (respondeu);
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><inputtype="checkbox" name="responderam" id="responderam"  onchange="Filtra(this)"  /> Mostrar apenas quem respondeu
    
02.08.2017 / 14:39
2

Another alternative

$("#responderam").change(function() {
  var respondeu = "";
  var valor = $("#responderam").val();
  if(this.checked) {
    respondeu = 1;
    alert(respondeu);
  } else {
    respondeu = 0;
    alert(respondeu);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><inputtype="checkbox" name="responderam" id="responderam" value="1"  /> Mostrar apenas quem respondeu
    
02.08.2017 / 14:37