gpt4 book ai didi

jquery - 在列表中过滤 Jquery 中的结果

转载 作者:行者123 更新时间:2023-12-01 03:19:56 26 4
gpt4 key购买 nike

我从互联网上下载并编辑了一段代码,基本上我想做的是创建一个我使用 JSON 从数据库中读取的字符串列表。该列表构建得很完美,现在我想做的是使用 Jquery 过滤结果(文本框)。最好的方法是什么?当用户在文本框中键入内容时显示完整的结果列表,过滤列表中的这些结果。

谢谢

$(function ()
{
$.ajax({
url: 'api.php', data: "", dataType: 'json', success: function(rows)
{
var list = $("#toggle").append('<ul></ul>').find('ul');
for (var i in rows)
{
var row = rows[i];
//var id = row[0];
var Dname = row[4];
Dname = Dname.toLowerCase();
list.append("<li>"+Dname+"</li><div>Pulse</div>");
}
$('ul li:odd').addClass('zebra_odd');
$('ul li:even').addClass('zebra_even');
$("li").click(function(){
$(this).toggleClass("active");
$(this).next("div").stop('true','true').slideToggle();
});
}
});
});

最佳答案

这非常有效:

$('input').keyup(function() {                       // Bind keyup event to textbox
var textboxVal = $(this).val().toLowerCase(); // Get value of textbox
$('ul li').each(function() { // loop through the list
var listVal = $(this).text().toLowerCase(); // get value of the <li>
if(listVal.indexOf(textboxVal) >= 0) { // search if textboxVal is in listVal
$(this).show(); // if true show this <li>
} else {
$(this).hide(); // else hide this <li>
}
});
});

示例:http://jsfiddle.net/PWAXt/

关于jquery - 在列表中过滤 Jquery 中的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11041972/

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