Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
JQuery usando .html para pasar datos ajax en la tabla

Tengo un problema al pegar algo de html en un div. Creo que sé lo que estoy haciendo mal, pero no puedo encontrar una solución a esto. Tengo un div vacío llamado "allbrands". Este div se llena con datos de una llamada POST ajax después de hacer clic en un botón. Hice un bucle que recorre los datos, pero reemplaza el html constantemente. Entonces, ¿cómo puedo pegar esta tabla html sin reemplazar cada vez el contenido?

Mi división:

 <div id="allbrands"> </div>

Mi Ajax:

 <script type="text/javascript"> $( document ).ready(function() { $('[name="getbrands"]').click(function (e){ e.preventDefault(); $.ajax({ type: "GET", url: '/brands/all', success: function (data) { for(var i = 0; i < data.length; i++) { $('#allbrands').html('<h6>' + data[i] + '</h6><hr>'); } } }); }); }); </script>

Los datos se llenan así:
ingrese la descripción de la imagen aquí

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Cambia esta línea:

 $('#allbrands').html('<h6>' + data[i] + '</h6><hr>');

a

 $('#allbrands').append('<h6>' + data[i] + '</h6><hr>');

e intenta de nuevo.

Explicación: html() reemplaza el contenido anterior agregando uno nuevo, append() retiene el anterior y agrega uno nuevo también.

over 4 years ago · Santiago Trujillo Report

0

El problema está en tu bucle. Está anulando el valor de div en cada iteración. Debe agregar el valor en lugar de anularlo:

Prueba esto:

 for(var i = 0; i < data.length; i++) { $('#allbrands').append('<h6>' + data[i] + '</h6><hr>'); }
over 4 years ago · Santiago Trujillo Report

0

Siga el siguiente enlace para obtener más información.

http://api.jquery.com/append/

También agregue el

 $("#allbrands").append();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!