How to insert the id of the data? but to show me the name in the select2

1

In the select I already show the name of the data 'json', but I want to send to the db its id.

 <section class="col col-6">
  <label class="label"> Nombre </label>
   <select name="room_id" id="room_id" class="form-control">
   </select>      
 </section>

$.each(data.room_options, function(key, value) {   
     $('#room_id')
         .append($("<option/>")
               .attr("value",key.room_options)
               .text(value.name));                        
     $('#room_id').select2();
});  
    
asked by afr 09.03.2017 в 21:00
source

2 answers

0

With this I got what I wanted

.text(value.name).val(value.id));
    
answered by 17.03.2017 / 00:49
source
1

How about, I think this is what you wanted to achieve.

var data = {}

data.room_options = {
  "Single": "habitación simple",
  "Double": "habitacion doble"
}

$.each(data.room_options, function(key, value) {   
     $('#room_id')
         .append($("<option>")
               .attr("value",key.room_options)
               .text(value));                        
     $('#room_id').val("habitacion doble");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

 <section class="col col-6">
  <label class="label"> Nombre </label>
   <select name="room_id" id="room_id" class="form-control">
   </select>      
 </section>
    
answered by 09.03.2017 в 22:31