jQuery UI 自动完成显示在列表上显示结果,但选择时打印值而不是标签

发布于 2024-10-17 22:04:34 字数 1467 浏览 3 评论 0原文

我在自动完成工作方面遇到很多问题。首先,我遇到了验证不兼容的问题,因为我使用的是 jQuery Validation 和 jQuery 1.5,并且破坏了 Autocomplete 小部件上的 AJAX 功能。这个问题已经解决了,现在我的 PHP 控制器返回一个带有标签/值的 JSON 结果。当然,label是我要显示的名称,value是我需要提交的值。

我正在使用 PHP 的 JSON 输出示例,仅用于测试,目前不用于搜索。这是我发出 AJAX 请求时发回的内容:

[{"label":"Client example","value":1},{"label":"Lorem Ipsum","value":2},{"label":"Microsoft","value":3}]

使用下一个代码,我认为它将直接向我显示标签并提交 id。当我开始输入时,我会在客户端示例、Lorem Ipsum 和 Microsoft 列表中看到我想要的结果,但是当我选择其中一个时,列表会关闭,输入会显示值而不是标签。

$("#clientid").autocomplete({
    source: function(request, response) {
        $("#ajax-loader").fadeIn('normal');

        $.ajax({
            url: BASE_URL + 'projects/ajax/get_clients',
            data: request,
            dataType: "json",
            type: "POST",
            success: function(data) {
                response(data);
            }
        });

        $("#ajax-loader").fadeOut("normal");
    },
    minLength: 2
});

我还尝试了 @3nigma 在我寻找自动完成失败原因时建议我的代码:

success: function(data) {
    response($.map( data, function( item ) {
        return {
            label: item.label,
            value: item.value
        }
    }));
}

什么也没有。查看 jQuery UI 网站和 Google 的示例,似乎 select 必须执行此操作,所以我尝试了:

select: function(event, ui) {
    $("#clientid").val(ui.item.label);
}

但也不起作用。,我看到 上的值。

我不知道我还能尝试什么。我该如何解决这个问题?

先感谢您!

I am having a lot of problems getting autocomplete working. First I've a validation incompatibility because I was using jQuery Validation and jQuery 1.5 and was breaking the AJAX functions on Autocomplete widget. This has been solved, now my PHP controller returns me a JSON of results with label/value. Of course, label is the name I want to show and the value is the value I need to submit.

I am using an example JSON output with PHP, only for testing and not for searching right now. This is what I send back when I make the AJAX request:

[{"label":"Client example","value":1},{"label":"Lorem Ipsum","value":2},{"label":"Microsoft","value":3}]

And using the next code I supposed that will show me directly the label and submit the id. When I start typing I see the results I want in a list Client example, Lorem Ipsum and Microsoft but when I select on of them the list is closed and the input shows the value and not the label.

$("#clientid").autocomplete({
    source: function(request, response) {
        $("#ajax-loader").fadeIn('normal');

        $.ajax({
            url: BASE_URL + 'projects/ajax/get_clients',
            data: request,
            dataType: "json",
            type: "POST",
            success: function(data) {
                response(data);
            }
        });

        $("#ajax-loader").fadeOut("normal");
    },
    minLength: 2
});

I also tried a code that @3nigma suggested me when I was looking why was the autocomplete failing:

success: function(data) {
    response($.map( data, function( item ) {
        return {
            label: item.label,
            value: item.value
        }
    }));
}

And nothing. Looking on examples of jQuery UI website and Google, seems that select has to do this thing so I tried:

select: function(event, ui) {
    $("#clientid").val(ui.item.label);
}

But doesn't works too., I see the value on the <input />.

I don't know what more I can try. How I can solve this problem?

Thank you in advance!

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

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

发布评论

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

评论(2

杯别 2024-10-24 22:04:34

单个<输入/>只能存储一个值,没有任何标签。

<input type="text" value="Lorem Ipsum" name="myInput">

自动完成只是设置输入的值属性,这与您输入的输入值完全相同。无法直接在输入中显示与其值不同的标签。

您需要调整数据源以返回与标签相同的值,

[{"label":"Client example","value":"Client example"}...

但您会丢失您的值。处理提交的表单时,您可以使用标签检索值,假设标签是唯一的。

A single <input /> can only store a value, without any label.

<input type="text" value="Lorem Ipsum" name="myInput">

Autocomplete simply sets the value attribute of the input, which is exactly the same as you would type in the input value. It is not directly possible to display a label in an input that would be different than its value.

You need to to adjust your data source to return value the same as label

[{"label":"Client example","value":"Client example"}...

but you will loose your value. When processing the submitted form you could retrieve the value using the label, assuming the label is unique.

梦开始←不甜 2024-10-24 22:04:34

我能够在 select: 和 focus: 函数中使用另外两行代码来完成此任务。

在你的情况下添加这些

    select: function(event, ui) {
    event.preventDefault();
    $("#clientid").val(ui.item.label);
    },
    focus: function(event, ui) {
    event.preventDefault();
    $("#clientid").val(ui.item.label);
    }

I was able to accomplish this using 2 additional lines of code in the select: and focus: functions.

in your case add these

    select: function(event, ui) {
    event.preventDefault();
    $("#clientid").val(ui.item.label);
    },
    focus: function(event, ui) {
    event.preventDefault();
    $("#clientid").val(ui.item.label);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文