0

我有一个简单的 JavaScript 客户端,它从 REST API 中提取一些书籍数据,但是我似乎无法调用函数 createBookRow(bookid) 并将适当的 html 字符串返回到调用它的文档就绪函数,

输出当前正在正确生成,已通过 html 页面上的 .row-fluid 附加验证,欢迎提出想法或建议

function createBookRow(bookid)
{
$.get('http://mysite.co.uk/atiwd/books/course/'+bookid+'/xml', function(xml){  

$(xml).find('book').each(function(){  
var $book = $(this); 
var id = $book.attr("id"); 
        var title = $book.attr("title");  
        var isbn = $book.attr("isbn");
        var borrowedcount = $book.attr("borrowedcount");
        var  html = '<div class="span3"><img name="test" src="http://covers.openlibrary.org/b/isbn/'+isbn+'-L.jpg" width="32" height="32" alt=""></p>' ; 
         html += '<p> ' + title + '</p>' ;  
         html += '<p> ' + isbn + '</p>' ;  
         html += '<p> ' + borrowedcount + '</p>' ;  
         html += '</div>';  
        $('.row-fluid').append($(html));  
    });         
}); 
}



 $(document).ready(function()  
  {  
    $.get('xml/courses.xml', function(xml){  
    $(xml).find('course').each(function(){ 

        var $course = $(this); 
        var id = $course.attr("id"); 
        var title = $course.text();  
        var html = '<div class="span12"><p>' + title + '</p><row id="'+id+'" >'+createBookRow(id)+'</row></div>' ;  
        $('.row-fluid').append($(html));   
        $('.loadingPic').fadeOut(1400); 
    });  
});


});  
4

2 回答 2

0

您提供的代码中没有返回任何内容。您只需将一些 HTML 附加到 jQuery 对象。尝试添加返回语句

return html;
于 2013-02-27T11:53:14.133 回答
0

线

 var html = '<div class="span12"><p>' + title + '</p><row id="'+id+'" >'+createBookRow(id)+'</row></div>' ; 

应该只是

 var html = '<div class="span12"><p>' + title + '</p><row id="'+id+'" ></row></div>' ; 
 createBookRow(id);

createBookRow(id) 函数正在发出获取请求以获取一些详细信息,这是异步发生的。除非您明确提及它是同步调用(不建议这样做)。

I guess the functionality you need is to render some rows for course and in between you need books details displayed. In that case you need to explicitly say where your book row needs to be appended.

 $('.row-fluid').append($(html));

The above code will always append book row at the end.

于 2013-02-27T12:02:51.377 回答