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)
– NoobSaibot
It says "Undefined"
– WitnessTruth
That’s weird. This Function Filter() is in the script.js file and is not being called directly on the page.
– WitnessTruth
The element
#responderam
exists at the moment it tries to use?– BrTkCa
@Lucascosta agree, if it exists, it works. https://jsfiddle.net/jwvh3eo0/
– Oralista de Sistemas
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.
– WitnessTruth
Actually there is another element with the same id on the page?
– Oralista de Sistemas
No, only it has this name. I have tried with different names but still will not.
– WitnessTruth
@Renankaiclopes the reply that Leandro sent is working normal, so also I left another answer as an alternative.
– NoobSaibot
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 });
– BrTkCa
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?
– WitnessTruth
@Renankaiclopes You even tried according to the 2nd method I posted in my answer to see if it works correctly?
– Leandro