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

162
Views
Cambiar una verificación de interruptor de palanca a verdadero o falso

Tengo un interruptor de palanca como este: ingrese la descripción de la imagen aquí

Y la etiqueta Active proviene de una base de datos. Quiero que este botón se cambie a true si está activo.

Esto es lo que he probado hasta ahora:

HTML

 <input onclick='toggleStatus(this)' type='checkbox' id='{{ db.id }}' name='{{ db.id }}'> <label id='user-status-toggle' for='{{ db.id }}'>{{ db.status }}</label>

JavaScript

 function toggleStatus(radio) { var toggle = document.getElementById('user-status-toggle'); if (radio.checked) { toggle.textContent = "Active"; radio.checked = true; } else { toggle.textContent = "Inactive"; radio.checked = false; } }

Está funcionando, pero solo cuando interactúo con el botón. Cuando visito la página por primera vez, siempre está desactivada, incluso si el estado es Activo. Tenga en cuenta que puedo tener varios botones y su estado puede variar entre Active e Inactive .

Entonces, ¿cómo debo hacer esto?

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

0

Puede configurar la checked inicial en su <input />

simplemente agregue el atributo checked .

por ejemplo,

 // if this one is what you want initial checked.. <input checked onclick='toggleStatus(this)' type='checkbox' id='{{ db.id }}' name='{{ db.id }}'> <label for='{{ db.id }}'>{{ db.status }}</label> <input onclick='toggleStatus(this)' type='checkbox' id='{{ db.id }}' name='{{ db.id }}'> <label for='{{ db.id }}'>{{ db.status }}</label>

No conozco bien PHP, pero supongo que funcionaría.

 // if this one is what you want initial checked.. <input checked='{{ db.textContent === "Active" }}' onclick='toggleStatus(this)' type='checkbox' id='{{ db.id }}' name='{{ db.id }}'> <label for='{{ db.id }}'>{{ db.status }}</label>
about 4 years ago · Juan Pablo Isaza Report

0

Lo arreglé verificando si db.status === "Active" primero con una declaración if-else en PHP:

 if (db.status === "Active") { return <input checked onClick=toggleStatus(this)'> } else { return <input onClick=toggleStatus(this)'> }
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!