Jquery 迭代om表中的每一行并进行api调用
我试图遍历表中的每一行,并使用book和author值进行api调用,获取一些细节并填充字符串。 我这里的问题是,当我点击submit时,我的str_tot是空的。当我添加alert语句以找出发生这种情况的原因时,。每个函数似乎一次从表中获取所有值,然后只使用最后一个值进入api调用循环 api调用进行调用并获取信息有1秒的延迟。这有点奇怪,我用尽了我的选择试图找出问题。非常感谢您的帮助Jquery 迭代om表中的每一行并进行api调用,jquery,json,api,Jquery,Json,Api,我试图遍历表中的每一行,并使用book和author值进行api调用,获取一些细节并填充字符串。 我这里的问题是,当我点击submit时,我的str_tot是空的。当我添加alert语句以找出发生这种情况的原因时,。每个函数似乎一次从表中获取所有值,然后只使用最后一个值进入api调用循环 api调用进行调用并获取信息有1秒的延迟。这有点奇怪,我用尽了我的选择试图找出问题。非常感谢您的帮助 <table id="list"> <thead> <th>Autho
<table id="list">
<thead>
<th>Author</th>
<th>Book</th>
</thead>
<tbody>
<tr>
<td class="check"><input type="checkbox" checked></td>
<td class="author">Name1</td>
<td class="book">Book1</td>
</tr>
...
...
...
<tr>
<td class="author">Name10</td>
<td class="book">Book10</td>
</tr>
</table>
<script>
var str_tot = " ";
$('#submit').click(function () {
$('#list > tbody > tr').each(function () {
var author = $(this).find('td.author').val();
var book = $(this).find('td.book').val();
if($(this).find('td.check').find("input").is(':checked')) {
var url = "http://api.book.com/some/link/search?query="+author+"&title[]=booktitle:"+book;
$.getJSON(url, function(data) {
var book = data.result[0]["book"];
var author = data.result[0]["author"];
});
str_tot = str_tot + " " + author + " " + book;
});
});
</script>
作者
书
名称1
第一册
...
...
...
名字10
第十册
var str_tot=“”;
$(“#提交”)。单击(函数(){
$('#list>tbody>tr')。每个(函数(){
var author=$(this.find('td.author').val();
var book=$(this.find('td.book').val();
if($(this.find('td.check')).find(“input”).is(':checked')){
变量url=”http://api.book.com/some/link/search?query=“+author+”&title[]=书名:“+book;
$.getJSON(url、函数(数据){
var book=data.result[0][“book”];
var author=data.result[0][“author”];
});
str_tot=str_tot+“”+作者+“”+书籍;
});
});
AJAX异步调用服务,因此在调用$.getJSON
方法后,它将立即转到下一条指令,即
str\u tot=str\u tot+“”+作者+“”+书籍;
因此,将上述语句移到回调函数中
$.getJSON(url, function(data) {
var book = data.result[0]["book"];
var author = data.result[0]["author"];
str_tot = str_tot + " " + author + " " + book;
});
很抱歉代码有误导性。这正是你所指出的。我不能在这里发布代码,所以我不得不重写它,并且在编写代码时犯了一个错误。