0

Tenho uns campo campos imputs, com valores. Fiz a página adicionar assim:

AJUDA QUE PRECISO: Na página de editar, ao carregar a página seja listado as opções com checkbox marcados.

$(".c").on('change', function() {

  var values = $(this).val().split('|');
  if ($(this).prop('checked')) {

    var quantity = $("table[id^=tabelaIE]").length;

    $(this).data('qty', quantity);

    var table = $("#tabelaIE").clone(true)
      .attr('id', function() {
        return this.id + quantity;
      })
      .find(':text,:file')
      .attr('id', function() {
        return this.id + quantity;
      })
      .val("")
      .end();

    //VALUE -> 1|Taxa de locação|ENC|15.00|FIX|POR|VLT|%
    //$ID_ImpEnc|$nome|$tipo|$valor|$calculo|$valor_em|$aplicacao|$a

    //APLICAÇÃO: [SEG] Sobre seguro, [KML] KmLivre, [DIA] Diária, [IE] Impostos/Encargos, [VLT] no valor total
    //VALOR EM.: [MOT] Montante, [POR] Porcentagem.
    //CALCULO..: [DIA]Por dia, [FIX]Fixo
    //TIPO.....: [TXV]Taxa de Venda, [ENC]Encargos Extras

    aplicacao = values[6];
    valor_em = values[5];
    calculo = values[4];
    tipo = values[2];

    if (valor_em == "MON" && calculo == "FIX") {
      table.find('.tNome').text(values[1]);
      table.find('.tDias').text("1");
      table.find('.tValor').text(values[3]);
      tTotal = values[3] * 1;
      table.find('.tTotal').text(tTotal.toFixed(2));
    }

    if (valor_em == "MON" && calculo == "DIA") {
      table.find('.tNome').text(values[1]);
      table.find('.tDias').text($("#dias").val());
      table.find('.tValor').text(values[3]);
      tTotal = values[3] * $("#dias").val();
      table.find('.tTotal').text(tTotal.toFixed(2));
    }

    if (valor_em == "POR" && calculo == "FIX") {
      if (aplicacao == "SEG") {
        $V = (50 + 30) / 100;
        table.find('.tNome').text(values[1]);
        table.find('.tDias').text("1");
        table.find('.tValor').text(values[3]);
        tTotal = values[3] * $V;
        table.find('.tTotal').text(tTotal.toFixed(2));
      }
      if (aplicacao == "KML") {
        $V = values[3] / 100;
        table.find('.tNome').text(values[1]);
        table.find('.tDias').text("1");
        table.find('.tValor').text(values[3]);
        tTotal = values[3] - $V;
        table.find('.tTotal').text(tTotal.toFixed(2));
      }
      if (aplicacao == "DIA") {
        $V = (values[3] * $("#dias").val()) / 100;
        table.find('.tNome').text(values[1]);
        table.find('.tDias').text($("#dias").val());
        table.find('.tValor').text(values[3]);
        tTotal = values[3] * $V;
        table.find('.tTotal').text(tTotal.toFixed(2));
      }
      if (aplicacao == "VLT") {
        $V = $("#totalFatura").val() / 100;
        table.find('.tNome').text(values[1]);
        table.find('.tDias').text("1");
        table.find('.tValor').text(values[3].substring(0, 2) + "%");
        tTotal = values[3] * $V;
        table.find('.tTotal').text(tTotal.toFixed(2));
      }
    }



    values.forEach(function(valor, index) {
      table.find('[class="split' + (index + 1) + '"]').val(valor)
    });

    table.appendTo('#abc');

    var oldVal = $('#somaTabelaIE').val();
    $('#somaTabelaIE').val(eval(oldVal || 0) + eval(tTotal));

  } else {

    tTotal = values[3] * $("#dias").val();
    var oldVal = $('#somaTabelaIE').val();
    $('#somaTabelaIE').val(oldVal - eval(tTotal));

    //remove a table que pertence ao checkbox
    $("table#tabelaIE" + $(this).data('qty')).remove();
  }
  $(".recalcula").click();
}).change();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Dias: <input type="text" value="2" id="dias"> Total da Fatura<input type="text" id="totalFatura" value="100.00" />
<p>===============================================</p>
<strong>OPÇÕES:</strong>
<div id="D1">1.
  <input name="opcoes[]" class="c Obrig" type="checkbox" checked value="1|Taxa de locação|ENC|15.00|FIX|POR|VLT|%" alt="15.00" title="Taxa de locação" /> <b> Taxa </b>
</div>
<div id="D2">2.
  <input name="opcoes[]" class="c " type="checkbox" checked value="2|GPS|ENC|5.00|DIA|MON|DIA|" alt="5.00" title="GPS" /> GPS
</div>
<div id="D3">3.
  <input name="opcoes[]" class="c " type="checkbox" value="3|Lavagem do carro|ENC|30.00|FIX|MON|VLT|" alt="30.00" title="Lavagem do carro" /> Lavagem
</div>
<div id="D4">4.
  <input name="opcoes[]" class="c " type="checkbox" checked value="4|Translado aéreo retirada|ENC|35.00|FIX|MON|VLT|" alt="35.00" title="Translado aéreo retirada" /> Translado
</div>


<p>===============================================</p>
<strong>LISTAS DAS OPÇÕES SELECIONADAS</strong>
<div id="abc">
  <table id="tabelaIE" width='400' border='0'>
    <tr>
      <td class="tNome" width='190px'></td>
      <td class="tDias" width='50px' align='center'></td>
      <td class="tValor" width='60px' align='right'></td>
      <td class="tTotal" width='100px' align='right'></td>
      <td class="tValores">
        <input type="hidden" class="split1" value="">
        <input type="hidden" class="split2" value="">
        <input type="hidden" class="split3" value="">
        <input type="hidden" class="split4" value="">
        <input type="hidden" class="split5" value="">
        <input type="hidden" class="split6" value="">
        <input type="hidden" class="split7" value="">
        <input type="hidden" class="split8" value="">
      </td>
    </tr>
  </table>
</div>
<p>===============================================</p>

Soma das opções:
<input type="text" class="somaTabelaIE" id="somaTabelaIE" value="0">

22
  • Podes explicar melhor o que queres dizer com "seja listado as opções com checkbox marcados"?
    – Sergio
    5/03/2019 às 22:46
  • Olá, Sérgio. Ao executar o código vai ver que tem checkbox marcados. Consegue ver?
    – Tiago
    6/03/2019 às 0:41
  • 1
    Não entendi, não era isso que você queria? 6/03/2019 às 5:31
  • Tiago, ainda não está claro o que precisas... quando este código que tens na pergunta carrega vejo já alguns checkbox marcados. O que te falta?
    – Sergio
    6/03/2019 às 7:07
  • @Sergio Atualmente o código só funciona se clicarmos no checkbox. Gostaria que ao carregar a página, verificasse os checkbox que estão marcados, carregar a lista e já fazer a soma total no imput somaTabelaIE
    – Tiago
    6/03/2019 às 11:56

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .