简体   繁体   English

单击第一项自动完成的jQuery jQuery UI

[英]Click on first item autocomplete jQuery ui

I'm trying to click on the first item of the list that displays autocomplete after searching: 我试图单击搜索后显示自动完成的列表的第一项:

$(function(){
    $('#autocompleteplace').each(function(i, el) {
        var $this = $(el);
        $this.autocomplete({
            source: $this.data('urlp'),
            autoFocus: true,
            minLength: 1,
            select: function( event, ui ) {
                $('#' + $this.data('id')).val(ui.item.id);
            }
        });
        var region = '{{ form.vars.data.place.region|default('') }}';
        $this.autocomplete("search", region, true);
        var menu = $this.autocomplete("widget");

        $(menu[0].children[0]).click();
    });
});

This code is the same from here , which I tried in the same environment and it works. 这段代码与这里的代码相同,我在相同的环境中尝试过,并且可以正常工作。 But in my example I get the data from the server, and it shows and selects the first item with autoFocus, but it doesn't click on it. 但是在我的示例中,我从服务器获取了数据,它显示并选择了带有autoFocus的第一项,但没有单击它。

I just want to edit a post that uses a field with autocomplete (place), so when the form is shown I want it to have the place already selected. 我只想编辑一个使用具有自动完成功能(位置)的字段的帖子,所以当显示表单时,我希望它已经选择了位置。

I asked for a quick fix of that problem: retrieve the value of the item that its already stored, but it's easier and cleaner doing it in the form. 我要求快速解决该问题:检索已经存储的项目的值,但是以表格的形式进行操作更容易,更干净。 My fix was modify the Transformer in my Symfony form. 我的解决方法是在Symfony表单中修改Transformer。 Since this is not a PHP topic, I'm not going into details. 由于这不是PHP主题,因此我不再赘述。

In conclusion: there should be a better way to achieve that than forcing a click() event. 结论:与强制click()事件相比,应该有更好的方法来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM