开发者

Loop through json object and catch variables on the fly (jquery)

开发者 https://www.devze.com 2023-01-07 20:57 出处:网络
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

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]);

:)

0

精彩评论

暂无评论...
验证码 换一张
取 消