javascript - JSON into Options of a Input Select -


i have string of json i'm trying put options of select input field. i'd type_label go in type_lable , value type number.

here json:

var servicecodes = [{ "type_label": "medical care", "type": "1",  }, { "type_label": "surgical", "type": "2",  }, { "type_label": "consultation", "type": "3",  }] 

here html , js

 <select id="datas" name="datas" >   <script>     var $select = $('#datas');      $.each(servicecodes, function(i, val){         $select.append($('<option />', { value: (i+1), text: val[i+1] }));      });  </script> 

for reason isn't working. appreciated.

thanks!

try this:

$select.append('<option value="' + + '">' + val.type_label + '</option>'); 

in cases, when you're confusing of how retrieve data in loop, take on arguments provided function(i use this):

console.log(arguments);


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -