gpt4 book ai didi

javascript - Ajax Response 添加在每个元素下

转载 作者:行者123 更新时间:2023-11-27 23:39:51 24 4
gpt4 key购买 nike

我有一个关于 Jquery Ajax Response 的问题,我似乎无法回答。

我使用 a foreach 从<p>中选择一些数据每个 info_div 中的标签。然后,我使用该数据对服务进行 ajax 调用,并使用 XML 进行回复。我想将此 XML 中的一些元素放置在我获取两个变量的每个 div 下。每个 div 都应该有自己的回复。

$(document).ready(function () {
$('#action-button').click(function () {
$(".info_div").each(function () {
var var1 = $(this).find('p:nth-child(4)').text();
var1 = var1.slice(-10);

var var2 = $(this).find('p:nth-child(8)').text();
var2 = var2.slice(-1);

$.ajax({
type: "GET",
url: "http://www.mypage.com/mypage&value1=" + "va1" + "&value2=" + "var2",
cache: false,
dataType: "xml",
success: function (xml) {
$(xml).find('member').each(function () {
var name = $(this).find("title").text()

/* how do I get this variable under each $('.info_div') from where I selected the var1 and var2
Every attempt I made places all replies under all the divs in class .info_div */

});
}
});
});
});
});

最佳答案

我不建议您循环发送ajax请求。最好收集所有数据,然后将其发送到服务器并处理响应。

同时,如果您坚持在循环中执行此操作,则应该从 $(".indo_div) 集合中引用迭代元素,并在 ajax 回调中使用它。

$(".info_div").each(function() {
var _that = $(this);
var var1 = $(this).find('p:nth-child(4)').text();
var1 = var1.slice(-10);

var var2 = $(this).find('p:nth-child(8)').text();
var2 = var2.slice(-1);

$.ajax({
type: "GET",
url: "http://www.mypage.com/mypage&value1=" + "va1" + "&value2=" + "var2",
cache: false,
dataType: "xml",
success: function(xml) {
$(xml).find('member').each(function(){
var name = $(this).find("title").text()
that.append(name);
});
}

});
});

关于javascript - Ajax Response 添加在每个元素下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com