There is a ComboBox when you select which transfers the received values ​​from the database using ajax to the second ComboBox, when 1 value arrives, everything is displayed correctly, but if 2 values ​​arrive, they are written in 1 line, and you need to add them to the next Options. I ask for your help

Code

var t = null; $( "#hotelid" ).change(function() { t = $( "#hotelid :selected" ); $.ajax({ type: 'GET', url: './modules/common.php', data: { fn: 'combobox', id: t.val() }, dataType: 'json', }).done(function(data, textStatus, jqXHR) { var list = data.list.rooms; var e =""; for (var i = 0; i < data.list.length; i++) { e += data.list[i].rooms; $( "#roomid option:selected").text(e); $( "#roomid option:selected").val(e); }; }); }); 

UP

Answer ajax

 {list: [{rooms: "Еще комната"}, {rooms: "тест на кол-во"}]} Эти 2 значения должны попасть в `<option value="value"></option>` сейчас пока только 1 показывает. 

html

 <select type="text" class="form-control" name="roomid" id="roomid"> <option value="value"></option> </select> 

    1 answer 1

     $( "#hotelid" ).change(function() { // simulate asynchronous ajax var response = {list: [{rooms: "Еще комната"}, {rooms: "тест на кол-во"}]}; setTimeout(function(data) { var room = $( "#roomid option:selected"); for (var i = 0; i < data.list.length; i++) { e = data.list[i].rooms; if (room.length == 0) { $("#roomid").append("<option></option>"); room = $("#roomid option").last(); } room.text(e); room.val(e); room = room.next(); } }, 1000, response); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select type="text" class="form-control" name="hotelid" id="hotelid"> <option value="value1">Royal</option> <option value="value2">Savoy</option> </select> <br/> <select type="text" class="form-control" name="roomid" id="roomid"> <option value="value"></option> </select> 

    • Thank you very much for the help, but the problem remained ... now it shows 1 value although it came 2. - Pavel
    • who is this "he"? - Igor
    • Himself in shock, here is the result from the base. id rooms 2 Another room 5 test for number result <option value = "Another room"> Another room </ option> - Pavel
    • Please add a relevant html to your question and a sample response to the ajax request, indicating where and what should go. - Igor
    • added as you requested. - Pavel