jquery ui自动完成更改事件 [英] jquery ui autocomplete change event

查看:288
本文介绍了jquery ui自动完成更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Jquery自动完成功能只允许选择项目。
i使用change事件检测:

I'm trying to make Jquery autocomplete to only allow selection items. i used the change event to detect that:

 change: function (event, ui) {
            if (ui.item) {
                $(this).val('');
            }
        }


,但是在chrome ui.item中,即使从列表中选择了项目,也是null。

and it works great in IE. but in chrome ui.item in allways null even if the item was selected from list.

然后我尝试了一种不同的方法。
并检查事件类型:

then i tried a different approach. and check the event type instead:

 if (event.originalEvent.type != "autocompletechange") {
            $(this).val('');
        }

它在Chrome中很好用,但在IE中的事件是Blur。

and it works great in Chrome but in IE the event is 'Blur'.

有一个适合两种浏览器的解决方案

is there a solution that fits both browsers??

$ b

推荐答案

我有同样的问题,不得不解决它。似乎没有解决这个问题。

I had the same problem and had to work around it. There doesn't seem to be a fix to this.

jQuery的自动完成功能会传送事件 ui 对象只在选择事件。在更改事件没有对象被发送,不幸的是。

jQuery's autocomplete sends event and ui objects only on the select event. On change event no object is sent, unfortunately.

这是我做的,似乎工作顺利:

This is what I did and it seems to be working smooth:

$('#autocomplete-input').autocomplete({ 
    source : fields, // fields is an array of objects with field_name and field_id

    change : function() {

        var input = $('#autocomplete-input').val().trim();

        if ( input.length > 0 ) {

            var field_found = false;
            $.each( fields, function( index, value ) {
                if ( input == fields[index]['field_name'] )
                    field_found = fields[index]['field_id'];
            });

            if ( field_found ) {

                $('#field-selected-hidden').val( field_found ); 
            } else {

                $('#field-selected-hidden').val('');
            }

        } else {

            $('#field-selected-hidden').val('');
        }
    }
});

它基本上是:如果输入字段更改值,设置自动完成字段(源)。如果它的值等于新字段值改变为的值,它将隐藏的表单字段设置为字段的id(或任何您要捕获的字段)。

What it basically does is: if the input field changes value, it searches the array of the pre-set autocomplete fields (the source). If it has the a value equal to what the new field value has changed to, it sets the hidden form field to the id of the field (or whatever field you want to capture.)

希望它有助于...

这篇关于jquery ui自动完成更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆