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

189
Views
Select2 js - Cómo crear una opción al escribir en el cuadro de búsqueda

Estoy usando select2 para crear una opción ilimitada para seleccionar, lo que significa que cada vez que escribe algo en el cuadro de búsqueda de select2, puede crear una opción relacionada con la selección. Ahora estoy usando el código a continuación, que cuando se escribe crea más de 1 opción para la que lo necesito.

Por favor, ayúdame con esto.

 $('#form-label-category-exterior').select2({ matcher: function matchExterior(params, data){ if ($.trim(params.term) === '') { return data; } if (typeof data.text === 'undefined') { return null; } if (data.text.indexOf(params.term) > -1) { var modifiedData = $.extend({}, data, true); return modifiedData; } else{ var newOpt = new Option(params.term.toString() + '外装', params.term.toString() + '外装', true, true); $('#form-label-category-exterior').append(newOpt) } return null; } })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede ver la fuente de esta función en el siguiente enlace. Pero, según tengo entendido, solo puede agregar una opción adicional aquí. Así que creo que esto no es lo que quieres.

Enlace fuente

Así que tengo una solución diferente si quieres usar. El código que escribí crea una nueva opción y elimina la opción anterior que creó mi código. Traté de explicar todo. Espero que entiendas el punto.

 $(document).ready(function() { let oldValue = ""; $('#form-label-category-exterior').select2({ matcher: function matchExterior(params, data){ if ($.trim(params.term) === '') return data; if (typeof data.text === 'undefined') return null; if (data.text.indexOf(params.term) > -1) { return $.extend({}, data, true); } else { const newText = params.term.toString() + '外装'; var newOpt = new Option(newText, newText); $(`#form-label-category-exterior option[value="${oldValue}"]`).remove() $('#form-label-category-exterior').append(newOpt) const $return = data.id == params.term.slice(0, -1) + '外装' oldValue = newText; $('#form-label-category-exterior').trigger('change'); return $return ? {id: newText, text: newText } : null; } return null; } }) $('#form-label-category-exterior').on('select2:closing', function (e) { oldValue = "" }); });
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!