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

115
Views
Se hace clic en el botón de verificación ajax desde una lista de botones rellenada dinámicamente

Hola, soy nuevo en ajax y tengo una respuesta json de django con una lista de nombres que se mostrarán como una lista de botones. Quiero deshabilitar el botón una vez que se hace clic.

A continuación se muestra el código html que escribo actualmente, el botón se muestra correctamente pero no realiza la acción del botón de desactivación.

También me gustaría preguntar cuál es el alcance de esta función onclick . ¿La función muere al final de la llave } ? ¿O esta función verifica incluso si está fuera del alcance?

 <h3>User List:</h3> <ul id="userLists"> </ul>
 $(document).ready(function(){ $.ajax({ type:"GET", // getUserList is a django views where it query the database and returns JsonResponse of the user's name url: "{% url 'getUserList' %}", success: function(response){ $("#userLists").empty(); // Create a list according to number of users in json for (var key in response.users){ var temp="<button type=\"button\", name=\""+ response.users[key].user +"\", class=\"btn btn-outline-danger\">" + response.users[key].user + "</button>"; $("#userLists").append(temp); // Here I want to do when clicked, it disables the button and leave others as is. // Current implementation does not work... document.getElementsByName(response.users[key].user).onclick = function(){document.getElementsByName(response.users[key].user).prop('disabled', true)}; }; } }); })

También probé la siguiente implementación, no funciona...

 if(document.getElementsByName(response.user[key].user).clicked == true){ document.getElementsByName(response.user[key].user).prop("disabled", true); };

Gracias a todos.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

getElementsByName devuelve una lista de nodos... Entonces, una colección de elementos coincidentes.
Es por eso que .onclick y .prop("disabled", true) no tienen efecto.

si response.users[key].user debe ser único, puede usar de forma segura document.getElementsByName(response.user[key].user)[0] .
Así que agregue [0] para apuntar a la primera coincidencia.

Acerca de las comillas , puede que le interese leer sobre los literales de plantilla .

about 4 years ago · Juan Pablo Isaza 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!