jQuery AutoComplete,如何接受 Found &未找到的条款

发布于 2024-09-25 07:17:34 字数 419 浏览 0 评论 0原文

我引用了本教程中看到的 jQuery 自动完成插件代码: http://net. tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/

本教程的问题是它仅支持在服务器上找到的项目。我想做的是允许用户使用服务器上找到的项目(就像今天一样),但也允许用户输入新值而不破坏插件...示例,这样您就可以输入用户的电子邮件地址,按 Enter 键,然后继续使用该插件,也许然后在服务器上找到另一个项目并再次按回车键。

有想法吗?可能的?

I'm referencing the jQuery autocomplete plugin code seen in this tutorial:
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/

The problem with this tutorial is that it only support items found on the server. What I would like to do is allow a user to use items found on the server (as it works today) but also to allow user to input new values without the breaking the plugin... Example, so you could enter a user's email address, press enter, and then continue using the plugin, perhaps then finding another item on the server and again hitting return..

Ideas? Possible?

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

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

发布评论

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

评论(1

谜兔 2024-10-02 07:17:34

您可以尝试将输入的内容附加到建议列表中。这样他们基本上可以使用“req.term”选择他们正在输入的内容。像这样:

//process response
$.each(data, function(i, val){                              
    suggestions.push(val.name);
});
//append what has been typed in so it's available for selection
suggestions.push(req.term);
//pass array to callback
add(suggestions);

然后,在 select: 函数中,您可以使用 ajax 调用将选择插入到数据库中(如果该选择尚不存在)。

//define select handler
select: function(e, ui) {

    //create formatted friend
    var friend = ui.item.value,
        span = $("<span>").text(friend),
        a = $("<a>").addClass("remove").attr({
            href: "javascript:",
            title: "Remove " + friend
        }).text("x").appendTo(span);

    //add friend to friend div
    span.insertBefore("#to");

    //insert selected email if doesn't already exists
},

这是一个按键示例,用于在输入时插入格式化的朋友:

$("#to").keypress(function(e){
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) { //Enter keycode
        e.preventDefault();

        //create formatted friend
        var friend = $(this).val(),
            span = $("<span>").text(friend),
            a = $("<a>").addClass("remove").attr({
                href: "javascript:",
                title: "Remove " + friend
            }).text("x").appendTo(span);

        //add friend to friend div
        span.insertBefore("#to");

        $(this).autocomplete('close');
    }
});

You could try appending what is being typed in to the list of suggestions. That way they can essentially select what they are typing using "req.term". Like this:

//process response
$.each(data, function(i, val){                              
    suggestions.push(val.name);
});
//append what has been typed in so it's available for selection
suggestions.push(req.term);
//pass array to callback
add(suggestions);

Then, in the select: function, you could insert the selection into the database with an ajax call if it doesn't already exists.

//define select handler
select: function(e, ui) {

    //create formatted friend
    var friend = ui.item.value,
        span = $("<span>").text(friend),
        a = $("<a>").addClass("remove").attr({
            href: "javascript:",
            title: "Remove " + friend
        }).text("x").appendTo(span);

    //add friend to friend div
    span.insertBefore("#to");

    //insert selected email if doesn't already exists
},

Here's a keypress example for inserting you formated friend on enter:

$("#to").keypress(function(e){
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) { //Enter keycode
        e.preventDefault();

        //create formatted friend
        var friend = $(this).val(),
            span = $("<span>").text(friend),
            a = $("<a>").addClass("remove").attr({
                href: "javascript:",
                title: "Remove " + friend
            }).text("x").appendTo(span);

        //add friend to friend div
        span.insertBefore("#to");

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