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

104
Views
Evento de disparo solo cuando se marca una casilla de verificación sin marcar

Tengo esta función de JavaScript que solo quiero activar si se marca una casilla sin marcar y mi solicitud de obtención es verdadera. Al alertar, hasCoPrimary es verdadero, pero la instrucción if nunca se activa. Supongo que esto se debe a que la casilla de verificación no se registra como 'marcada' hasta después de que pasa la declaración if. ¿Cómo hago para que la función espere a que se registre el cambio de la casilla de verificación para poder usarla en una instrucción if?

 $('#btn-isPrimary').on('change', function (e) { $.get('/Ingredient/CheckForCoPrimary/', { code: "@Model.Code", id: @Model.Id }, function (hasCoPrimary) { //Only shows modal if the item being made primary already has another ingredient with the same code marked as primary if ($(this).is(':checked') && hasCoPrimary === true) { $("#primary-modal").modal(); } }); });

EDITAR: Se cambió el código a esto y ninguno sigue funcionando. Tengo una alerta que me dice qué es hasCoPrimary antes de pasar a la declaración if, y es True cada vez, por lo que esa parte es correcta.

 $('#btn-isPrimary').on('change', function (e) { var checkbox = $('#btn-isPrimary'); $.get('/Ingredient/CheckForCoPrimary/', { code: "@Model.Code", id: @Model.Id }, function (hasCoPrimary) { alert(hasCoPrimary); //Only shows modal if the item being made primary already has another ingredient with the same code marked as primary if (checkbox.is(':checked') && hasCoPrimary === true) { $("#primary-modal").modal(); } }); });

También probé esto

 $('#btn-isPrimary').on('change', function (e) { var checkbox = $('#btn-isPrimary'); $.get('/Ingredient/CheckForCoPrimary/', { code: "@Model.Code", id: @Model.Id }, function (hasCoPrimary) { alert(hasCoPrimary); //Only shows modal if the item being made primary already has another ingredient with the same code marked as primary if (e.currentTarget.checked && hasCoPrimary === true) { $("#primary-modal").modal(); } }); });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El problema es que $(this) ya no es la casilla de verificación una vez que está dentro de su función de devolución de llamada $.get : debe establecer una variable antes de su función de devolución de llamada o, mejor aún, puede usar e.currentTarget.checked = as está pasando e a su función de cambio, e.currentTarget es la casilla de verificación que ha cambiado

 $('#btn-isPrimary').on('change', function(e) { $.get('/Ingredient/CheckForCoPrimary/', { code: "@Model.Code", id: @Model.Id }, function(hasCoPrimary) { //Only shows modal if the item being made primary already has another ingredient with the same code marked as primary if (e.currentTarget.checked && hasCoPrimary === true) { $("#primary-modal").modal(); } }); });

Sin embargo, si solo tiene la intención de mostrar el modal si la casilla de verificación está marcada, entonces movería la parte marcada de la declaración if fuera de $.get para que no haga llamadas ajax innecesarias:

 $('#btn-isPrimary').on('change', function(e) { if (e.currentTarget.checked) { $.get('/Ingredient/CheckForCoPrimary/', { code: "@Model.Code", id: @Model.Id }, function(hasCoPrimary) { //Only shows modal if the item being made primary already has another ingredient with the same code marked as primary if (hasCoPrimary === true) { $("#primary-modal").modal(); } }); } });

Si lo anterior no se incluye en if (hasCoPrimary === true) , es posible que desee verificar si hasCoPrimary es un bool o una cadena; si está alertando como True como en su edición, es posible que deba probar if (hasCoPrimary === 'True') . También tenga en cuenta la pequeña c en currentTarget

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!