Jquery ischecked does not work

Asked

Viewed 85 times

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 update 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 pick up the value "1" checkbox even if I check it. I am using Jquery 1.8.

What am I doing wrong? I’ve seen answers to other questions but it doesn’t work.

  • Below answered = 1 / 0 ; put: Alert(value)

  • It says "Undefined"

  • That’s weird. This Function Filter() is in the script.js file and is not being called directly on the page.

  • 1

    The element #responderam exists at the moment it tries to use?

  • @Lucascosta agree, if it exists, it works. https://jsfiddle.net/jwvh3eo0/

  • It does not identify the element #responded. It does not say that it does not exist but I also cannot get any value from it.

  • Actually there is another element with the same id on the page?

  • No, only it has this name. I have tried with different names but still will not.

  • @Renankaiclopes the reply that Leandro sent is working normal, so also I left another answer as an alternative.

  • The problem can be three: 1) Having no element with the id answered. 2) There is more than one element with the id answered . 3) The element id answered not exist at the time of use, for this case the solution is $(document).ready(function() { // checked teste aqui });

  • 1

    Bruno’s solution. H worked. If I try the $("#answered") selector it does not work and does not identify the element (but also does not give error), only if I pass the value as parameter to the function. What can it be?

  • @Renankaiclopes You even tried according to the 2nd method I posted in my answer to see if it works correctly?

Show 7 more comments

3 answers

4


It is working normally, follows example displaying a alert according to the state 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>
<input type="checkbox" name="responderam" id="responderam" value="1" onchange="Filtra()"  /> Mostrar apenas quem respondeu

2nd alternative - calling the function by type of the input case on your page does not exist others 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>
<input type="checkbox" name="responderam" id="responderam" value="1"/> Mostrar apenas quem respondeu

3rd alternative - maintaining the 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>
<input type="checkbox" name="responderam" id="responderam" value="1"/> Mostrar apenas quem respondeu

  • 1

    That is, if there is the element #responderam, what seems to be the problem of AP.

  • The second alternative worked, Leandro. But I really need to check if the checkbox is marked in other functions and unfortunately there is no way I can use . change to set the value. But I don’t know why I can’t get it if it’s selected or not because it comes as Undefined.

  • @Renankaiclopes But the .change has the same effect and works in the same way as onchange="Filtra()" used in your code, the only difference is that you will not have to place the function call onchange within the input.

  • 1

    I ended up deciding to redo the entire function by writing from scratch and now it worked and it’s picking up the value of the input #responded. The function I put in the question was only a part of Filtra(). I don’t know what I ended up changing that in the end everything went right. But thanks for the accompaniment Leandro.

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>
<input type="checkbox" name="responderam" id="responderam"  onchange="Filtra(this)"  /> Mostrar apenas quem respondeu

  • The solution per hour was this. If I try the $("#answered") selector it does not work and does not identify the element (but also does not give error), only if I pass the value as parameter to the function. What can it be?

  • 1

    Well I’m sorry, I don’t domino jQuery to tell you why, I only used a little of what I know to try to help you.

2

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>
<input type="checkbox" name="responderam" id="responderam" value="1"  /> Mostrar apenas quem respondeu

Browser other questions tagged

You are not signed in. Login or sign up in order to post.