Checkbox field is not recognized marked

Asked

Viewed 38 times

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 he doesn’t recognize that he’s marked, when I make one console.log($("#Produtos")); console.log($("#Servico")); He always returns false to me, I can’t understand.

  • 3

    # looking for id and none of the inputs has a id defined

  • 3

    Knowing if it’s marked is done with $("#Produtos").is(":checked")

  • That’s exactly what I wanted, thank you @Isac

1 answer

3


You can pass the current selector to the function using the 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>

<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(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>

Browser other questions tagged

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