Checkbox in the Console

Asked

Viewed 70 times

0

I created a form with some registration information as a task in my internship job.

This information must be printed in the Console, the problem is in 3 checkbox boxes that I cannot display the selected option in the console.

html.

<div class="col-md-2">
  <h5><strong>Serviço:</strong></h5>
  <div class="form-check">
    <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
    <label class="form-check-label" for="defaultCheck1">
      Suporte
    </label>
  </div>
  <div class="form-check">
    <input class="form-check-input" type="checkbox" value="" id="defaultCheck2">
    <label class="form-check-label" for="defaultCheck2">
      Desenvolvimento
    </label>
  </div>
  <div class="form-check">
    <input class="form-check-input" type="checkbox" value="" id="defaultCheck3">
    <label class="form-check-label" for="defaultCheck3">
      Infraestrutura
    </label>
  </div>
</div>

Javascript:

function enviar() {

    //Variaveis que recebem valor dos inputs e depois são atribuidas ao JSON
    var data = $("#inputData").val();
    var nome = $("#inputNome").val();
    var cep = $("#inputCep").val();
    var endereco = $("#inputEndereco").val();
    var cidade = $("#inputCidade").val();
    var estado = $("#inputState").val();
    var bairro = $("#inputBairro").val();
    var cnpj = $("#txtCnpj").val();
    var preço = $("#inputPreco").val();
    var codigo = $("#txtCodigo").val();
    var servico = $("#defaultCheck1").val();

    //checkbox
    var formValue = {
        data: data,
        name: nome,
        endereço: endereco,
        cep: cep,
        cidade: cidade,
        estado: estado,
        bairro: bairro,
        cnpj: cnpj,
        preço: preço,
        codigo: codigo,

    };

    console.log(formValue);
}

1 answer

1


You need to check the property checked and not value.

Since you are using jQuery, you can use the method prop.

$('#bt').on('click', function () {
  const c = $('#defaultCheck2').prop('checked')
  console.log(c)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" id="defaultCheck2"><br>
<button id="bt">Testar</button>

  • worked!! and how can I return "True" on the console to the text of the checkbox ??

  • @Great. I think that would be a problem for another question, because it involves other operations from there. By the way, if the answer has solved your problem, you can mark it as correct using the V on her left side (only one per question). And welcome to [pt.so].

  • Thank you so much for the support, I’m new to development and Stack Overflow!

Browser other questions tagged

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