2016-12-15 4 views
1

Мне нужно заполнить выбор на основе ранее взятых данных автозаполнения. Итак, у меня есть эти 2 поля HTML:Заполнять выбор из Ajax

<div id="ind_ritiro" style="display:none"> 
          <legend>Indirizzo ritiro</legend> 
           <div class="form-group"> 
            <label for="exampleInputEmail1">Ragione sociale</label> 
            <input type="text" class="form-control" id="rs_triangolazione" 
              name="rs_triangolazione" value="" placeholder="Digita ragione sociale"> 
           </div> 
           <div class="form-group"> 
            <label for="exampleInputPassword1">Indirizzo</label> 
            <select id="add_triangolazione" name="add_triangolazione" class="form-control"> 
             <option value=""></option>  
            </select> 
           </div> 



    <script type="text/javascript"> 
$(document).ready(function() { 
    var source = document.getElementById("rs_triangolazione").value; 
    $("#rs_triangolazione").autocomplete({ 
    minLength:3, 
    autoFocus: true, 
    source: '{{URL('triangolazione')}}', 
    select: function(event, ui) { 
     $("#rs_triangolazione").val(ui.item.id); 
     $("#rs_triangolazione").val(ui.item.label); 

    }, 

}); 
}); 

</script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
    $("#add_triangolazione").change(function(){ 
     $.ajax({ 
      type: 'POST', 
      url: 'indirizzi-triangolazione', 
      data: {azienda:$('#rs_triangolazione').val()}, 
      success: function (response) { 
      document.getElementById("new_select").innerHTML=response; 
       } 
     }); 
    }); 
}); 
</script> 

2 фрагмента выборки, а также все данные, которые мне нужны. Но теперь мне нужно установить данные, выбранные в select ... Я полагаю, мне нужна функция .each, но я не могу пройти ...

ответ

0

Создайте свой новый выбор, а затем нажмите на него свои данные с помощью $.each()

$('#ind_ritiro').append('<select id="new_select"></select>'); 

$.each(response,function(index){ 
    $('#new_select').append('<option>',{ 
     value: response[index].valueYouWant, 
     text: response[index].valueYouWant 
    }); 
}); 
Смежные вопросы