Check checkbox only when checked

Asked

Viewed 928 times

2

I am checking a check box to see if you have a type="radio" selected. The problem that check is running the calculation even when I uncheck the check box. I did the following test to see what was happening.

console.log($("input[type='checkbox'][id='SavePaper']").is(':checked') );

The test result was:

true false

false true

true false

Follow the image

inserir a descrição da imagem aqui

Why it happens and how to solve?

Updating: checkbox code

<div class="md-checkbox" onclick="getValue()">
     <input type="checkbox" id="SavePaper" name="SavePaper" class="md-radiobtn">
      <label for="SavePaper">
          <span class="inc"></span>
          <span class="check"></span>
          <span class="box"></span>
      </label>
</div>

  • It has nothing to do directly with your question, but given that the id’s should be unique in the DOM (page) because you simply don’t: $('#SavePaper').is(':checked');. On second thought your problem may come from there. The two buttons even though they have the same type, cannot have equal id’s. fix this and see if the problem remains

  • gave exactly the same thing

  • I’ll put checkbox code...

  • I updated the comment above. I didn’t say I would solve the problem, I just said I would be better

  • They are not with equal ids

  • The checkbox is with one and the radio is with another

  • You should put the code you have (preferably functional, in snipet) so that it is easier to help you

Show 3 more comments

1 answer

0

$("#SavePaper").click(function(e) {
     if($(this).is(':checked')){
         if ( $("input[type='radio'][name='Licenciamentodeusuários']").is(':checked') ){
             valor = $('input:radio[name=Licenciamentodeusuários]:checked').val();

             if(valor == "Até") {

                 if ($("#Ate-Comercial").val() != undefined) {
                     qtdUsuarios = $("#Até-Comercial").val();
                     vlrFinal = qtdUsuarios * 0.99;
                     console.log(vlrFinal);
                 }
                 if ($("#Até-Educacional").val() != undefined) {
                     qtdUsuarios = $("#Até-Educacional").val();
                     vlrFinal = qtdUsuarios * 0.99;
                     console.log(vlrFinal);
                 }
                 if ($("#Até-Profissional").val() != undefined) {
                     qtdUsuarios = $("#Até-Profissional").val();
                     vlrFinal = qtdUsuarios * 0.99;
                     console.log(vlrFinal);
                 }
             }else{

                 valor = valor.replace(/\s{2,}/g, ' ');
                 valor = valor.split(" ");

                 vlrFinal = valor[2] * 0.99;
                 console.log(vlrFinal);

             }
         }

     }

 });

Solved.

  • Could add an explanation of what was done to correct the error

Browser other questions tagged

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