javascript - how to get json data using jquery -
i generating radio button depends on jsondata id , name. below code not working.how resove issue.
json:
{ "a": [ { "id" : "a1", "family" : "family" }, { "id" : "a2", "family" : "family" }, { "id" : "a3", "family" : "family" } ], "b": [ { "id" : "b1", "family" : "family" }, { "id" : "b2", "family" : "family" }, { "id" : "b3", "family" : "family" } ], "c": [ { "id" : "c1", "family" : "family" }, { "id" : "c2", "family" : "family" }, { "id" : "c3", "family" : "family" } ] }
javascript:
generatefamily("a"); function generatefamily(objt) { var objct = objt; $.getjson("./js/result.json", function(result) { var testobj = result + "." + objct; $.each(testobj, function(i, field) { var char = "a"; char = "ln-" + char; $("#dfour").append("<li class=" + char + " style='display: list-item;'><a href='#'><span class='m'><input type='radio' class='fbol_ff' value=" + field.id + " family=" + field.family + "/></span>" + field.family + "</a></li>"); }); }); }
you nee read a
field json data. since passing a
in variable i.e. objt
. can fetch using bracket notation
var testobj=result[objt];
instead of
var testobj = result + "." + objct;
var result = { "a": [{ "id": "a1", "family": "family" }, { "id": "a2", "family": "family" }, { "id": "a3", "family": "family" }], "b": [{ "id": "b1", "family": "family" }, { "id": "b2", "family": "family" }, { "id": "b3", "family": "family" }], "c": [{ "id": "c1", "family": "family" }, { "id": "c2", "family": "family" }, { "id": "c3", "family": "family" }] }; generatefamily("a"); function generatefamily(objt) { var testobj = result[objt]; $.each(testobj, function(i, field) { var char = "a"; char = "ln-" + char; $("#dfour").append("<li class=" + char + " style='display: list-item;'><a href='#'><span class='m'><input type='radio' class='fbol_ff' value=" + field.id + " family=" + field.family + "/></span>" + field.family + "</a></li>"); }); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='dfour'></div>
Comments
Post a Comment