我有 JSON,看起来像这样:
/主题/创建/示例
[
{
"user_id": 1,
"name": "sample",
"created_by": "Sample96",
"created_time": "2012-09-10T20:44:11",
"num_of_followers": 1,
"id": 5
}
]
现在在我的 JS 中,我想要这样的东西:
function fnClickAddRow(data) {
$("#s").append(data['name']); //it doesn't work
}
Html 看起来像这样:
<p id="s">Subject : </p>
更新:
JS
$(document).ready(function (){
add_subject_to_subjects_list();
subject_description();
data_tables();
});
/* DataTables */
function data_tables() {
var oTable = $('.book-grid').dataTable({
});
}
function fnClickAddRow(data) {
var result = data[0].user_id;
$("#s").append(result);
}
/* PERSONAL_PAGE.HTML */
function add_subject_to_subjects_list() {
show_subjects_list();
$("#btn-create-subject").click(function() {
var value = $("#id_name").val();
var link = "/subject/create/"+value;
$.post(link, function(data) {
show_subjects_list();
fnClickAddRow(data);
});
});
}
function show_subjects_list() {
$("#list").text('');
$.get("/subject/list/", function(data){
for( var i=0; i<data.length; i++) {
var subject = data[i]['name'];
var followers = data[i]['num_of_followers']
// var link = "/user/"+username+"/subject/"+subject;
$("#list")
.append('<li>'+subject+'<span id="followers"> ['+followers+'] </span></li><br>');
};
}, 'json');
}