List Bank result in checkbox with AJAX and JQuery

1

I have a function that searches the database for an entity list and shows it in select. However, I need this same list to be shown in a checkbox where the user can select more than one option. Showing this list of results in < input type="checkbox" name="" value="" id "" & gt ;?
How do I get the uploaded values checked?

Current JS assembles the list in a < Select & gt ;, I need to replace to check:

 function CarregaEntrada() {
         $.ajax({
             url: "/Qualidade/Entidade/CarregaEntidade",
                //data: { representante: representante },
                async: false,
                success: function (data) {
                    $("#entrada").empty();
                    $("#entrada").append('<option value="0" disabled selected hidden>Selecione...</option>');

                    $.each(data, function (i, element) {
                        $("#entrada").append('<option value=' + element.Id + '>' + element.Descricao + '</option>');
                    });

                }
            });
        }

    });

current html - (need to override select by check)

  <div class="col-md-10">
       <select class="form-control select2" id="entrada" name="entrada"></select>
      </div>
    
asked by anonymous 05.06.2018 / 15:43

1 answer

2

Then to get the result of the query Ajax and generate the checkboxes :

$.each(data, function (i, element) {
    $('#divEntradas').append('<input type="checkbox" name="entrada" id="' + element.Id +'" />' + element.Descricao);
});

To submit the form that has checkboxes, you can use serialize of JQuery to help:

var form = $('#id_do_form');

$.ajax({
  type: "POST",
  url: form.attr('action'),
  data: form.serialize(),
  success: function(resposta) {
     console.log(resposta);
  }
});

Here I took the form's own action as a url, but could replace it with another specific url.

    
05.06.2018 / 18:57