Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
Implementar la opción exclusiva en selección múltiple: borre otras entradas al seleccionarla, borre al seleccionar otras

Tengo una selección múltiple donde una opción debe ser exclusiva:

  1. Si se selecciona, cualquier otra selección anterior en la selección múltiple se deselecciona
  2. Si se selecciona otra opción mientras está seleccionada, esta opción se deselecciona

Estaba tratando de examinar la opción recién seleccionada, pero .val() me da la selección general.

 $('#educationLevel').on('change', function(e) { var selection = $(e.target).val(); // This gives overall selection, not the just-selected one for my checks }

¿Algun consejo?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Hay un hilo similar aquí, ¿Exclusión mutua para <opciones> en un <select>? . Necesitamos realizar un seguimiento de la matriz de selección actual y compararla con la última matriz de selección. Esto nos dirá qué se acaba de seleccionar.

Solución:

 // Global Var: // To implement an exclusive option in a MultiSelect dropdown, // we need to keep track of the current selection (a global var here) // and diff this array with the latest selection array in change() event. var currSel = $('#educationLevel').val(); // Change Event Definition $('#educationLevel').on('change', function() { var newSel = $(this).val(); // Get diff from currSel; a 1-element array expected. // Diff in arrays: see: https://stackoverflow.com/a/33034768/1005607 var diff = newSel.filter(x => !currSel.includes(x)); // If exclusive option just got selected, delete all others if (diff.length) { if (diff[0] === '405') { // '405' is my exclusive option // If exclusive option just got selected ("405"), deselect all others // in modified selection array newSel = ['405']; } else { // If non-exclusive option just got selected, ensure that the exclusive option // is not in the modified selection array newSel = newSel.filter(el => el !== '405'); } // Set dropdown to the modified new selection array $('#educationLevel').val(newSel); } // Clone latest selection array into current selection array currSel = [...newSel]; }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda