jQuery 通过文本字段搜索数组

发布于 2024-12-15 07:41:53 字数 399 浏览 8 评论 0原文

我正在努力弄清楚如何使这项工作适用于大学项目,一些帮助将是惊人的!

我有一个存储大量项目的 mysql 数据库。 (我可以做到这一点没有问题)。

我有一个搜索框,需要在您键入时搜索项目数据库。 (我确定您知道我的意思?有点像谷歌即时搜索)

当您单击从建议项目下拉的可能列表中的项目时。 它通过 jquery 附加一个无序列表以在其中添加项目。

但是我不知道该怎么做。

我做了一个jquery小提琴http://jsfiddle.net/OwenMelbz/QW7Bc/意思是而不是在此处粘贴代码。

希望你能帮忙。

谢谢

欧文

I'm struggling to figure out how to make this work for a university project, some help would be amazing!!

Ive got a mysql database which stores lots of items. (i can do this bit no problem).

Ive got a search box which needs to search the item database as you type. (im sure you know what i mean? bit like the google instant search)

When you click the item from the list of possibles that drops down from the suggested items.
it appends a unordered list via jquery to add the item there.

however I've got no idea how to go about this.

i've made a jquery fiddle http://jsfiddle.net/OwenMelbz/QW7Bc/ of what i mean instead of pasting code here.

hope you can help.

thanks

Owen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

您检查过 jQuery UI Autocomplete 和那里的示例吗?

http://jqueryui.com/demos/autocomplete/

这应该可以帮助您解决“搜索为你输入”部分。

Have you checked out jQuery UI Autocomplete and the examples there?

http://jqueryui.com/demos/autocomplete/

That should help you out with the "search as you type" part.

掌心的温暖 2024-12-22 07:41:53

您可能想要这样的东西:

$("#text").keyup(function() {
    var input = $("#text").val();
    $.post("someurl/myService.cgi", {
        searchTerm: input
    },
    function(data) {
        $.each(data, function(index, value) {
            $("#list").append("<li>" + value + "</li>");
        });
    },
    'json'
});

不过,在将数据发布到服务器之前,您应该对输入进行一些验证。使用 $.data() 缓存一些搜索结果也可能是一个想法,除非它堆积太多,以避免过多的服务器命中。您可以使用 $("#list").empty() 在搜索之间从建议列表中删除所有项目。

You probably want something like this:

$("#text").keyup(function() {
    var input = $("#text").val();
    $.post("someurl/myService.cgi", {
        searchTerm: input
    },
    function(data) {
        $.each(data, function(index, value) {
            $("#list").append("<li>" + value + "</li>");
        });
    },
    'json'
});

You should do some validation on the input before you post data to the server though. It might also be an idea to cache some of the search results with $.data() unless it piles up too much, to avoid too many server hits. You can use $("#list").empty() to remove all items from the suggestion list between searches.

他不在意 2024-12-22 07:41:53

尝试查看 http://jqueryui.com/demos/autocomplete/

该页面上有一个示例这会根据您输入的内容从数据库中获取结果。我假设您可以从那里获取它(获取单击的项目并将其添加到您的页面/块)。
我希望我正确理解了你的问题。

Try looking at http://jqueryui.com/demos/autocomplete/

There is an example on that page that brings down results from db based on what you type. I assume you can take it from there (get the clicked item and add it to your page/block).
I hope I understood your question right.

木緿 2024-12-22 07:41:53

为懒惰的学生更新了小提琴。 (啊啊,美好的旧时光^^)

updated fiddle for the lazy student. (ahhh the good old times ^^)

爱她像谁 2024-12-22 07:41:53
$(function() {
    $('#list').hide();
    $("#find").click(function(){
        $('#list').toggle();
    });
    $('#find').bind('keyup', function(){
        $('ul#list li').css({display : 'none'});
        $('#list li').each(function(){
            var e = $(this).text().toLowerCase();
            if (e.indexOf($('#find').val().toLowerCase()) != -1){
                $(this).css({display : 'block'});
            }
        });
    });
});

用这个替换你的js。当您键入时,它会填充 ul。注意:要查看它的工作原理,请确保一直进行到第 2 项,因为它与字符匹配,您将看到所有 3 个结果,直到有一个字符与其中任何一个都不匹配。

$(function() {
    $('#list').hide();
    $("#find").click(function(){
        $('#list').toggle();
    });
    $('#find').bind('keyup', function(){
        $('ul#list li').css({display : 'none'});
        $('#list li').each(function(){
            var e = $(this).text().toLowerCase();
            if (e.indexOf($('#find').val().toLowerCase()) != -1){
                $(this).css({display : 'block'});
            }
        });
    });
});

replace your js with this. it will populate the ul as you type. NOTE: to see it work make sure you make it all the way to item 2 since it matches characters you will see all 3 results until you have a character that does not match any of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文