javascript - read dynamic data from json -
read json 'var dt='{"var1":"1","var2":"2"}';'
data every 1 second, in json data i've given static data 1,2, in program var1, var2 value change dynamically. need post dynamic data in text boxes, i've tried set interval function, no use:( needed,
$(document).ready(function() { $.ajaxsetup({cache: false}); var dt='{"var1":"1","var2":"2"}'; var data=$.parsejson(dt); if (data.var1) { $('#c1-cycle').val(data.var1); } if (data.var2) { $('#c2-cycle').val(data.var2); } });
try json.parse(dt);
instead of $.parsejson(dt);
. if var1
, var2
changes, need use:
$.each(dt, function (key, value) { // key => var1 or whatever latest // value => value stored in key if (key == "var1") { $('#c1-cycle').val(dt[key]); } if (key == "var2") { $('#c2-cycle').val(dt[key]); } });
if based on number get, change cx
value, can this:
$.each(dt, function (key, value) { // key => var1 or whatever latest // value => value stored in key $('#c' + key.replace('var', '') + '-cycle').val(dt[key]); });
updated comments , fiddle:
$(document).ready(function() { $.ajaxsetup({cache: false}); var winterval = setinterval(function () { var dt='{"var1":"1","var2":"2"}'; var data = json.parse(dt); $.each(data, function (key, value) { $('#c' + key.replace('var', '') + '-cycle').val(data[key]); }); }, 1000); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div> <input type="text" id="c1-cycle" /> </div> <hr /> <div> <input type="text" id="c2-cycle" /> </div>
Comments
Post a Comment