处理 jquery 自动完成中的粘贴值

发布于 2024-12-09 06:10:03 字数 1926 浏览 1 评论 0原文

抱歉,如果之前讨论过这个问题,我尝试搜索网络/stackoverflow,但找不到正确的答案。

我在一些表单上使用 jquery 自动完成插件,用户可以在其中输入一些数据。通常,数据以标准 ID-名称对表示。通过自动完成功能查找名称,选择后将相应的 ID 写入隐藏表单字段,以便稍后在后端处理。我通过对 php 后端的 JSON 调用获取数据。

我已经处理了自动完成上的选择和更改事件,以将选定的 ID 写入隐藏字段,并在未找到匹配项时将其删除。这有效。

问题是:当用户将正确的名称粘贴到自动完成中(确实存在的名称),但没有单击下拉选项时,我该如何处理?基本上 select 事件不会触发,并且我被输入的正确名称和不包含相应 ID 的隐藏字段所困扰? 我想我应该以某种方式遍历可用选项并选择一个(如果匹配),但不幸的是我不知道如何执行此操作。我正在尝试使用自动完成作为标准组合框,但带有...自动完成。

我希望我说清楚了,jQuery 对我来说还是个新东西,所以我会一边学习一边学习术语:)

谢谢。

更新

嗯,有点找到了解决方案,但并不完全满意。当更改事件触发时,如果未选择该项目,我将循环遍历 JSON,尝试查找用户输入的术语。

change: function(event, ui) {           
    if (ui.item) 
        return false;

    $("#airport_end_id").val("");
    var entered_value = $("#airport_end").val();                    
    if (entered_value != "") {
        $.each(airports, function(key, airport) {
            if (airport.value == entered_value) {
                $("#airport_end_id").val(airport.id);
                return false;
            }
        });
    };                
    return false;
},

我刚刚学习 JavaScript/jQuery,所以这段代码可能看起来像一大堆......:) 对此不满意,因为它可能会非常低效(json 应包含大约 2000-4000 行,目前它对于我的测试数据来说非常小)。我更愿意循环遍历属于该字段的隐藏 ui-autocomplete ul (它已经针对用户输入的单词进行了过滤),但由于我有几个包含相同数据的自动完成字段,我不知道如何找到 ui -autocomplete ul 对应于焦点字段。

基本上,两个自动完成字段的隐藏 ul 是完全相同的:

<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: -875px; left: 164px; display: none; position: relative; width: 300px;"></ul>

<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: -875px; left: 164px; display: none; position: relative; width: 300px;"></ul>

是否可以添加 ID 或其他内容来自动完成 UL,以便我可以追踪它?

再次感谢您的帮助。

Sorry if this was discussed before, I tried searching the web/stackoverflow but couldn't find a right answer.

I'm using jquery autocomplete plugin on some of my forms where the user can enter some data. Usually the data is represented in a standard ID-name pair. The name is looked up through the autocomplete and on selecting I write the corresponding ID into hidden form field to later process on the backend. I get the data from a JSON call to my php backend.

I've handled select and change events on the autocomplete to write selected ID into hidden field and delete it if no match was found. This works.

The question is: how do I handle when a user pastes a right name into autocomplete (a name that really exists), but doesn't click on the dropdown option? Basically select event doesn't fire and I'm stuck with a right name entered and a hidden field that doesn't contain corresponding ID?
I guess I should somehow loop through the available options and select one if it matches but unfortunately I don't know how to do this. I'm trying to use autocomplete as a standard combobox but with...autocomplete.

I hope I was clear, jQuery is still new to me so I'm picking up terms as I go along :)

Thank you.

UPDATE

Well, kinda found a solution but not entirely happy with it. When change event fires, if the item is not selected, I'm looping through my JSON trying to find the term that the user has entered.

change: function(event, ui) {           
    if (ui.item) 
        return false;

    $("#airport_end_id").val("");
    var entered_value = $("#airport_end").val();                    
    if (entered_value != "") {
        $.each(airports, function(key, airport) {
            if (airport.value == entered_value) {
                $("#airport_end_id").val(airport.id);
                return false;
            }
        });
    };                
    return false;
},

I'm just learning JavaScript/jQuery so this code might look like a big pile of ... :)
Not happy with it since it will probably be horribly inefficient (json should contain about 2000-4000 rows, currently it's very small with my test data). I would much rather loop through a hidden ui-autocomplete ul that belongs to this field (which is already filtered for the words a user entered) but since I have several autocomplete fields that contain the same data i don't know how to find ui-autocomplete ul that corresponds to the focused field.

Basically hidden ul's for two autocomplete fields are totally the same:

<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: -875px; left: 164px; display: none; position: relative; width: 300px;"></ul>

<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: -875px; left: 164px; display: none; position: relative; width: 300px;"></ul>

Is it possible to add an ID or something to autocomplete UL so that I can track it down?

Thanks again for any help.

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

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

发布评论

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

评论(2

情释 2024-12-16 06:10:03

您可以查看 autocompletechange 事件。当字段失去焦点并包含新值时,即使未在自动完成下拉列表中选择它,它也会触发。

$( ".selector" ).autocomplete({
   change: function(event, ui) { ... }
});

You could look into the autocompletechange event. It will fire when the field loses focus and contains a new value, even if it wasn't selected in the autocomplete drop down.

$( ".selector" ).autocomplete({
   change: function(event, ui) { ... }
});
蓝海 2024-12-16 06:10:03

通过使用 .autocomplete("widget") 并将特定类添加到 ui-menu 来解决这个问题。

Worked this out by using .autocomplete("widget") and adding specific class to ui-menu.

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