Field checkbox is not recognized marked

1

I have these two fields checkbox

 <label class="control-label col-md-3" style="text-align:left;">Tipo de Pedido</label>
 <div class="col-sm-3" style="text-align:left;">
      <input type="checkbox" asp-for="Produtos" onclick="checkProduto();"/>
      <label asp-for="Produtos" class="control-label"></label>
      <span asp-validation-for="Produtos" class="text-danger"></span>
 </div>
 <div class="col-sm-3" style="text-align:left;">
      <input type="checkbox" asp-for="Servico" onclick="checkServico();"/>
      <label asp-for="Servico" class="control-label"></label>
      <span asp-validation-for="Servico" class="text-danger"></span>
 </div>

But it does not recognize that it is checked, when I make a console.log($("#Produtos")); console.log($("#Servico")); It always returns me false, I can not understand.

    
asked by anonymous 15.10.2018 / 20:27

1 answer

3

You can pass the current selector to the function using this

function checkProduto(chekProduto) {
  console.log($(chekProduto).is(':checked'))
}


function checkServico(chekServico) {
  console.log($(chekServico).is(':checked'))
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><labelclass="control-label col-md-3" style="text-align:left;">Tipo de Pedido</label>
<div class="col-sm-3" style="text-align:left;">
  <input type="checkbox" asp-for="Produtos" onclick="checkProduto(this);" />
  <label asp-for="Produtos" class="control-label"></label>
  <span asp-validation-for="Produtos" class="text-danger"></span>
</div>
<div class="col-sm-3" style="text-align:left;">
  <input type="checkbox" asp-for="Servico" onclick="checkServico(this);" />
  <label asp-for="Servico" class="control-label"></label>
  <span asp-validation-for="Servico" class="text-danger"></span>
</div>
    
15.10.2018 / 20:45