How to append json values to html[value]

时间:2015-06-26 09:50:35

标签: javascript jquery json

I have Json like this. How to append the json values into html input values.

    [{"user_id":"180",
    "firstname":"anandhsp",
    "lastname":"sp",
    "email":"xyz@gmail.com",
    "mobile":"9000000000",
    "gender":null,
    "hashcode":"2XXg3dfyuxjO9C4OvaWw",
    "username":"anandhsp21",
    "password":"64c20f8bb630eb5cb329fdd609c807b7:J6",
    "emailverify":"TRUE",
    "company_name":"xxx",
    "address":"Chennai",
    "city":"Chennai",
    "state":"Tamilnadu",
    "pincode":"637001",
    "phone":"1234567890",
    "website":"hello",
    "nature":"hello",
    "no_employe":"23",
    "year":"2015",
    "type":"Proprietor",
    "authorized_person":"Anandh Sp",
    "status":"",
    "created":"2015-06-26 10:48:09",
    "modified":"2015-06-11 11:24:39",
    "logdate":"2015-06-26 05:18:09",
    "lognum":"3",
    "reload_acl_flag":"0",
    "is_active":"1",
    "extra":"N;",
    "rp_token":null,
    "rp_token_created_at":null,
    "app_name":"",
    "api_key":""}]

Html code

<div id="register_form" class="fieldset subgroupregister_form">
<div class="hor-scroll">
<table class="form-list" cellspacing="0">
<tbody>
<tr class="tr_tag">
<tr class="tr_application_id">
<tr class="tr_customer_id">
<tr class="tr_company_name">
<tr class="tr_address">
<td class="label">
<td class="value">
<input id="address" class=" input-text required-entry" type="text" value="" name="address">
</td>
</tr>
<tr class="tr_city">
<tr class="tr_state">
<tr class="tr_pincode">
<tr class="tr_mobile">
<tr class="tr_phone">
<tr class="tr_website">
<tr class="tr_nature">
<tr class="tr_no_employe">
<tr class="tr_year">
<tr class="tr_type">
<tr class="tr_authorized_person">
<tr class="tr_status">
</tbody>
</table>
</div>
</div>
</div>

I need to append the above values into input value

For example

<input id="address" class=" input-text required-entry" type="text" value="chennai" name="address">

I tried these Codes.But I did't got output.

jQuery('.ac_results ul li').bind('click',function(e)
                    {
                        var text = $(this).text();
                        jQuery.ajax({
                            type: 'get',
                            url: BASE_URL + 'admin/index/user_id',
                            data: {email: text},
                            dataType:'json',
                            success: function (data) {
                                var data = data[0];
                                $('#address').value = data.address;
                                $('#city').value = data.city;
                                $('#state').value = data.state;
                                $('#pincode').value = data.pincode;
                                $('#mobile').value = data.mobile;
                                $('#phone').value = data.phone;
                                $('#website').value = data.website;
                                $('#email').value = data.email;
                                $('#nature').value = data.nature;
                                $('#year').value = data.year;
                                $('#no_employe').value = data.no_employe;
                                $('#type').value = data.type;
                                $('#authorized_person').value = data.authorized_person;


                            }
                        });
                    });

Thanks In advance

1 个答案:

答案 0 :(得分:1)

Try val() function:

$('input').val(obj.item);

Check the following example

var obj = { test: 'test' }
$('#add').on('click', function() {
  $('#inp').val(obj.test);
});
$('#res').on('click', function() {
  alert($('#inp').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="inp" type="hidden" />
<button id="add">Add value</button>
<button id="res">Show input</button>