Loop through json object and catch variables on the fly (jquery)
Using jQuery I get JSON object that extracts one row from database. That row has about 30 columns so along with row data, success message (success = true) JSON result returns array with table columns (ex: id, name, address, etc) called table_columns.
The purpose is to place all the returned data into the relevant html form fields (for example: address in开发者_JAVA技巧put will be filled with data.adress) and I would like to descover the trick that lets loop through data.table_columns array and stores json data into its corresponding form inputs. Let me show you a code to explain it better:
$.getJSON(base_url+'index.php/iprdb/ajax_get_row/'+entry_id, function(data){
if(!data.success) {
alert('server error. please try again or contact support');
} else {
$.each(data.table_columns, function(i, item) {
var myVar = item; // gets table column name ex: address
$('#'+myVar).val(data.myVar); // stores address into input with id #address
});
}
});
so is it possible to access data.address for example using this method?
I have figures it:
var myVar = item;
$('#'+myVar).val(data[myVar]);
:)
精彩评论