1

Estou com um problema que recebo um informação em string em retorno de um ajax porém preciso deletar a linha especifica, mais para isso preciso criar um contador do elemento do append não estou conseguindo fazer isso vocês poderiam me ajudar

segue o código html:

<table class="table table-bordered">
            <thead>
                <tr>
                    <th style="background-color: #606060; color: white; text-align: center;"><b>Image</b></th>
                    <th style="background-color: #606060; color: white; text-align: center;"><b>Image Name</b></th>
                    <th style="background-color: #606060; color: white; text-align: center;"><b>Remover</b></th>
                </tr>
            </thead>
            <tbody id="imagelist" style="text-align: center;">

            </tbody>
        </table>

e o código javascript:

<script>
    var options = {
      shutter_ogg_url: "jpeg_camera/shutter.ogg",
      shutter_mp3_url: "jpeg_camera/shutter.mp3",
      swf_url: "jpeg_camera/jpeg_camera.swf",
    };
    var camera = new JpegCamera("#camera", options);

  $('#take_snapshots').click(function(){
    var snapshot = camera.capture();
    snapshot.show();

    snapshot.upload({api_url: "action.php"}).done(function(response) {
    var id = id++;

    $('#imagelist').prepend("<tr><td><img src='"+response+"' width='100px' height='100px'></td><td><input id='foto' style='border: transparent; background-color: transparent; text-align: center; width: 100%;' type='text' value='"+response+"_""'   /></td><td><button style='width: 50%; font-size:30px;' class='btn btn-danger' onclick='apagar();'><i class='glyphicon glyphicon-trash'></i></button></td></tr>");

}).fail(function(response) {
  alert("Upload failed with status " + response);
});
})

function done(){
    $('#snapshots').html("uploaded");
}
</script>
0

1 Resposta 1

0

Você não pode incrementar uma variável com o próprio valor ao mesmo tempo que a declara com var. O Resultado seria NaN, já que a variável não tem valor naquele instante.

Você precisa declarar a variável fora da função atribuindo-lhe um valor (por exemplo, 0), e na função fazer o incremento.

Analisando seu código, noto que você quer concatenar o valor do id neste value da string do prepend:

value='"+response+"_"+id+"'

Mas também terá que concatenar na id="foto", caso contrário estará inserindo vários elementos com o mesmo id="foto", o que é incorreto. Um id deve ser único na página. Logo terá que fazer também:

id='foto"+id+"'

O código ficará assim:

var options = {
   shutter_ogg_url: "jpeg_camera/shutter.ogg",
   shutter_mp3_url: "jpeg_camera/shutter.mp3",
   swf_url: "jpeg_camera/jpeg_camera.swf",
};
var camera = new JpegCamera("#camera", options);

var id = 0;
$('#take_snapshots').click(function(){
   var snapshot = camera.capture();
   snapshot.show();

   snapshot.upload({api_url: "action.php"}).done(function(response) {
      id++;

      $('#imagelist').prepend("<tr><td><img src='"+response+"' width='100px' height='100px'></td><td><input id='foto"+id+"' style='border: transparent; background-color: transparent; text-align: center; width: 100%;' type='text' value='"+response+"_"+id+"'   /></td><td><button style='width: 50%; font-size:30px;' class='btn btn-danger' onclick='apagar();'><i class='glyphicon glyphicon-trash'></i></button></td></tr>");

   }).fail(function(response) {
      alert("Upload failed with status " + response);
   });
})

function done(){
    $('#snapshots').html("uploaded");
}
2
  • Valeu irmão deu certo sua lógica
    – teste1234
    23/07/2018 às 14:00
  • Blz! Lembre-se que se deve marcar ✔️ na resposta que resolveu o problema.
    – Sam
    23/07/2018 às 14:07

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

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .