从Ajax中填充select

时间:2016-12-15 19:59:51

标签: jquery ajax

我需要根据以前提取的自动填充数据填充选择。所以我有这两个HTML字段:

<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函数,但是我无法通过...

1 个答案:

答案 0 :(得分:1)

创建新的选择,然后使用$.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
    });
});