1
Hello. I have a problem with the popular select with JSON. When the value has 2 or more words the value of my option is not filled correctly. Example:
{
"Marcas": {
"Acura": {
"Modelos": ["Integra", "Legend", "NSX"]
},
"Alfa Romeo": {
"Modelos": ["145", "147", "155"]
}
}
$.getJSON('models.json', function(data) {
$.each( data, function( key, val ) {
$.each(val, function(make, value){
console.log(make, value)
var option = '<option value='+make+'>'+make+'</option>';
$(option).appendTo("#marcas_list");
});
});
});
Results:
Note that the <option value="Alfa" romeo></option>
is being filled out incorrectly.
Something wrong with the code? Any suggestions ?
Hugs!
How am I supposed to show up? Test for example:
var option = '<option value="'+make.join(' ')+'">'+make.join(' ')+'</option>';
looks like what you’re looking for?– Sergio
good practice is when the key ( can be variable ) has more than one word use lowercase and together by a hyphen, thus prevents these problems , if you can change this part would avoid these problems.
– Marlysson
@Sergio when I try to use . Join(): Uncaught Typeerror: make.Join is not a Function
– caiolavos
Solution:
var $option = $('<option />')
 .prop('value', make)
 .prop('text', make);
 $("#marcas_list").append($option);
– caiolavos