gpt4 book ai didi

jquery - 在 jQuery 中提取 Ajax 返回数据

转载 作者:行者123 更新时间:2023-12-03 21:28:19 25 4
gpt4 key购买 nike

我已经完成了 jQuery 和 Ajax,但我无法将响应获取到 Div 元素中。这是代码:

Index.html

$.ajax({
type:"POST",
url: "ajax.php",
data:"id="+id ,
success: function(html){
$("#response").html(data);
}
});

它正在收到对我的 <div id="response"></div> 的回复.

ajax.php将以下代码返回到 index.html文件:

<div id ="one"> OneVal </div>
<div id ="sub"> SubVal </div>

我是否能够将 OneVal 和 Subval 提取到变量中,以及如何提取“OneVal”和“SubVal”,而不是上面的响应?

最佳答案

您可以在从响应创建的 jQuery 对象上使用 .filter:

success: function(data){
//Create jQuery object from the response HTML.
var $response=$(data);

//Query the jQuery object for the values
var oneval = $response.filter('#one').text();
var subval = $response.filter('#sub').text();
}

关于jquery - 在 jQuery 中提取 Ajax 返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/400197/

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