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

242
Views
AJAX lleno Select2 no se puede hacer clic

Estoy usando Select2 para un proyecto. El segundo cuadro de selección se llena según el elemento seleccionado en el primer cuadro, como se muestra en el enlace a continuación. Sin embargo, no puedo hacer clic en el primer elemento del segundo cuadro de selección por algún motivo. La única forma en que puedo seleccionar el primer elemento si quiero, es seleccionar primero un usuario diferente y luego volver al primero. ¿Como puedo resolver esto?

Video:

ingrese la descripción de la imagen aquí

Mi código:

Este es el primer cuadro de selección, que se llena con PHP normal (Laravel). Todo funciona bien aquí.

 <div class="form-group"> <label for="select"> Partner: </label> <select id="select" name="select" class="searchselect searchselectstyle"> @foreach($partners as $i => $partner) <option {{$i == 0 ? 'selected' : ''}} value="{{$partner->id}}">{{$partner->name}}</option> @endforeach </select> </div>

Aquí está el segundo cuadro de selección, con el error.

 <div class="form-group" > <label for="select2"> Hoofdgebruiker: </label> <select id="select2" style="min-width: 200px;" name="select2" class="searchselect searchselectstyle"> </select> </div> <script type="text/javascript"> $(document).ready(function(){ var url = '/json/getusers'; var $post = {}; $post.id = $("select").val(); $.ajax({ type: "POST", dataType: "json", url: url, data: $post, cache: false }).done(function(data){ $('#select2') .find('option') .remove() .end(); $.each(data, function(i, value) { console.log(data); $('#select2').append($('<option>').text(value.text).attr('value', value.id)); }); } ); });

-

 public function getusers(){ if(!isset($_POST['term'])){ $users = User::where('partner_id', $_POST['id'])->get(); }else{ $wildcard = '%'.$_POST['term'].'%'; $users = User::where('partner_id', $_POST['id'])->where('email', 'LIKE', $wildcard)->get(); } $array = array(); foreach($users as $i => $user){ $array[$i] = array("id" => $user->id, "text" => $user->email); } return response()->json($array); }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Verifique la distinción entre mayúsculas y minúsculas de la clave "id" en los datos json. Probablemente devuelva "ID" en lugar de "id".

 {"results":[{"id":"3","text":"Exampe 3"},{"id":"4","text":"Example 4"},{"id":"16","text":"Example 16"}]}

así no

 {"results":[{"ID":"3","text":"Exampe 3"},{"ID":"4","text":"Example 4"},{"ID":"16","text":"Example 16"}]}
over 4 years ago · Santiago Trujillo Report

0

Encontré una solución, que es la siguiente:

 <script type="text/javascript"> $(document).ready(function() { $(".searchselect").select2(); search(); $("#select").change(function(){ search(); }); }); function search(){ var $post = {}; $post.id = $("#select").val(); $("#select2").select2({ ajax: { dataType: "json", type: "POST", data: function (params) { var query = { term: params.term, id: $post.id }; return query; }, url: '/json/getusers', cache: false, processResults: function (data) { return { results: data }; } } }); } </script>

Ahora estoy usando la funcionalidad AJAX regular integrada en Select2, ¡todo funciona como se esperaba ahora!

over 4 years ago · Santiago Trujillo 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!