Dynamic checkbox does not show check arrow

Asked

Viewed 429 times

0

I have a checkbox that even selected does not show the checkbox item. This attribute is created dynamically. See image:

inserir a descrição da imagem aqui

When clicked it would have to be selected like this:

inserir a descrição da imagem aqui

Follow the code when I click the checkbox:

$(document).on("click",".preco-prod-adicional",function(e)
{
  e.preventDefault();

  if ($(this).attr('checked') == 'checked')
  {
    alert('selecionado');
    $(this).attr('checked',true);
    var precoproduto = "";
    var valortotal = "";
    var subtotal = 0;
    var resultadoitem = "";
    var replacetotal = "";

    precoproduto = $(this).val();
    valortotal = $(".total-v .v-lor").text();
    subtotal = somarValorTotal(precoproduto, valortotal);
    replacetotal = subtotal.replace(',','.');
    resultadoitem = $(".right-imagem .valor-total .total-v .v-lor").html(replacetotal);
  }
  else
  {
      $(this).attr('checked',false);
  }

});

HTML from the checkbox:

<input type='checkbox' class='preco-prod-adicional' name='checkadicional' value='" + precoprodadicional[0] + "' id='" + prodadicional[i] + "'/>
  • Add the checkbox html too, please. And pq you check when you have already tested and the element is checked?

  • <input type='checkbox' class='preco-Prod-additional' name='checkadditional' value='" + precoprodadditional[0] + "' id='" + prodadditional[i] + "'/>

  • I put because it does not appear checked when checking. It is created dynamically. Very strange!

  • Are you using common style or some other different style?

  • No. It is in a modal being created dynamically by the following variable: +=

  • Why change the state of checkbox in the click ? a checkbox already changes default state when clicking

Show 1 more comment

1 answer

0

If the idea is to do something when he stays checked or unchecked, better to use the event change and check the status of checked with .is(":checked")

Example:

$(".preco-prod-adicional").change(function(){
  
  if ($(this).is(":checked"))
  {
    //código para quando está checked
    console.log("Está checked");
  }
  else
  {
    //código para quando não está checked
    console.log("Não esta checked");
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' class='preco-prod-adicional' name='checkadicional' value='" + precoprodadicional[0] + "' id='" + prodadicional[i] + "'/>

Browser other questions tagged

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