php - 如何通过 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");
但没有成功。任何帮助,将不胜感激。
解决方案
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());
}
});
}
推荐阅读
- ios - 使 TextView 的一半文本颜色与其他 50% 的文本 SWIFT 不同
- javascript - 涉及当前日期的笑话测试功能
- c# - 是否可以为存储帐户中的所有容器获取单个共享访问签名?
- javascript - 如何使用 For 语句制作更简洁的代码?
- r - 将一个数据集与另一个数据集匹配并使用 R 分配相应的值
- python - AttributeError:导入sklearn库时模块'numpy'没有属性'testing'
- c# - 异步 NUnit 测试方法通过后等待调用
- vba - 我的 VBA 代码中的“标签未定义”是什么?
- node.js - 如何在环回中手动连接到 MongoDB 数据库?
- sql - 透视/转置标题表到行