在 jQuery 中提取 Ajax 返回数据 [英] Extracting Ajax return data in jQuery

查看:34
本文介绍了在 jQuery 中提取 Ajax 返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了 jQuery 和 Ajax,但我无法将响应放入 Div 元素中.这是代码:

Index.html

$.ajax({类型:POST",网址:ajax.php",数据:"id="+id ,成功:功能(html){$("#response").html(data);}});

它正在接收对我的<div id="response"></div>的响应.

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

OneVal </div><div id ="子">子值</div>

我能否将 OneVal 和 Subval 提取到变量中,如何提取OneVal"和SubVal"而不是上述响应?

解决方案

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

成功:函数(数据){//从响应 HTML 创建 jQuery 对象.var $response=$(data);//查询jQuery对象的值var oneval = $response.filter('#one').text();var subval = $response.filter('#sub').text();}

I have done jQuery and Ajax, but I am not able to get the response into a Div element. This is the code:

Index.html

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

It is receiving the response to my <div id="response"></div>.

The ajax.php returns following code to the index.html file:

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

Will I able to extract the OneVal and Subval into a variable, and how can I extract "OneVal" and "SubVal", instead of above response?

解决方案

You can use .filter on a jQuery object that was created from the response:

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 中提取 Ajax 返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆