使用 jqueryui 自动完成时的附加参数

发布于 2024-10-17 04:25:12 字数 572 浏览 8 评论 0原文

我希望向 jquery UI 自动完成请求添加一个附加参数,而不必将 json 返回嵌套在 ajax 调用中。我设想像下面这样工作,但是 data: 选项不会像普通的 jquery ajax 请求一样传递给 ajax 请求。

 $("#div").autocomplete({
        source: 'ajax.php',
        minLength: 2,
        data: '&action=getUserName',
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
});

tl;dr 我需要帮助将 &action=getUserName 传递给 ajax 以进行自动完成,最好不要将其嵌套在 ajax 回调中。

I'm looking to add an additional parameter to a jquery UI auto complete request without having to nest the json return in an ajax call. I would envision something like the following working, however the data: option is not passed over to the ajax request like it is on a normal jquery ajax request.

 $("#div").autocomplete({
        source: 'ajax.php',
        minLength: 2,
        data: '&action=getUserName',
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
});

tl;dr I need help passing &action=getUserName over to the ajax for my autocomplete, preferably without nesting it in an ajax callback.

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

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

发布评论

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

评论(1

幻想少年梦 2024-10-24 04:25:12

试试这个:

$("#div").autocomplete({
        source: 'ajax.php',

        extraParams: {
            action: function() {
                  return "getUserName";
            }
        },
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
});

根据文档: http://docs.jquery.com/Plugins/Autocomplete /autocomplete#url_or_dataoptions

编辑:添加了基于 jquery autocomplete extraParams 的更正

Try this instead:

$("#div").autocomplete({
        source: 'ajax.php',

        extraParams: {
            action: function() {
                  return "getUserName";
            }
        },
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
});

per docs: http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions

edit: added correction based on jquery autocomplete extraParams

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