javascript - Jquery get values from json into select -
i have json array looks this
[{ "": "select state/region" }, { "bal": "balkh" }, { "bam": "bamian" }]
i trying values out of using jquery, can't seem loop through.
this i've tried
var value = $(this).val(); var url = '{{ route('getstatesjson') }}'; $.getjson(url, { state: value }, function(obj) { (var = 0, len = obj.length; < len; i++) { } });
but can't seem pull of objects out. idea can use first value value , second value text in select.
you have below:-
$.getjson(url, { state: value }, function(obj) { var html = ''; $.each(obj,function(key,value){ $.each(value,function(k,v){ html +="<option value='"+k+"'>"+v+"</option>"; }); }); $('put select-box id or class').html(html); });
demo example:-
obj = [{ " ": "select state/region" }, { "bal": "balkh" }, { "bam": "bamian" }]; var html = ''; $.each(obj,function(key,value){ $.each(value,function(k,v){ html +="<option value='"+k+"'>"+v+"</option>"; }); }); $('#updateoptions').html(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="updateoptions"></select>
note:- think "": "select state/region"
need " ": "select state/region"
value option become empty value=" "
Comments
Post a Comment