首页 > 解决方案 > 如何通过 AJAX 将帖子发送到 PHP 并加载特定的 div

问题描述

我正在用 ajax 和 php 构建一个过滤器函数(我是 ajax 新手)。我有一个这样的 html 输入:

<form>
    <input type="text" placeholder="Search" onkeyup="getListSearch(this.value)" name="search_filter">
</form>

当前的函数 getListSearch 看起来像这样

function getListSearch(filter) {    
    $.ajax({
        method  : "POST",
        url     : "www.example.com/includes/content.php",
        data    : {
            search_filter: filter
        },
        success : function(result) {
            $("#list_content").html(result);
        }
    });
}

这可行,但会加载整个结果,我只想显示来自 content.php 的 .list_content 类的 div。我尝试了以下代码

$("#list_content").html($(result).find('.list_content'));
// Tried below also
$("#list_content").load(result + " .list_content");

但没有成功。任何帮助,将不胜感激。

标签: phphtmljqueryajax

解决方案


function getListSearch(filter) {    
    $.ajax({
        method  : "POST",
        url     : "www.example.com/includes/content.php",
        data    : {
            search_filter: filter
        },
        success : function(result) {
            var dom_nodes = $($.parseHTML(result));
            $("#list_content").html(dom_nodes.find('.list_content').html());
        }
    });
}



推荐阅读