English 中文(简体)
如何让jQuery自动完成弹出字段焦点?[副本]
原标题:How to get jQuery Autocomplete to pop up on field focus? [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
jQuery autocomplete UI- I d like it to start the search onfocus without the user having to type anything

jQuery UI自动完成

我希望这些选项在我的输入集中后立即出现。有这样的背景吗?我尝试将minLength设置为0,但不起作用。。。它仍在等待按键。

最佳回答
$("#yourField").bind( focus , function(){ $(this).autocomplete("search"); } );

Here a jsfiddle: http://jsfiddle.net/fpHf4/2/ Updated one (for IE): http://jsfiddle.net/q9ERL/4/

正如@HoldOffHunger所强调的,您还必须将minLength设置为0

问题回答

我认为你打破了“自动完成”的实用程序,只是做了一个风格化的选择,这就是等待按键完成的原因。

我知道这不是你想要的答案,只需记住这一点,你试图用很少的选项来完成工作,如果有很多选项,你会在第一个字母上获得艰难的自动完成div加载。

或者,如果是这样的话,你可以在sql查询上有10条结果记录,所以不用加载所有类型的结果就可以快速获取

---我在ie8上测试了焦点绑定,它失败了,顺便说一句,它没有失败,它完全按照你想要的方式在焦点上打开div框,不同的是IE在jquery焦点事件的情况下启动onFocus事件,而不像其他事件,所以su必须在焦点事件上评估if字段的空启动搜索,if不是什么都不做。。我希望这能有所帮助。

$("#yourField").bind( focus , function(){
  if($(this).val()!=""){ 
     $(this).autocomplete("search");
  } 
});

这里有一个解决方案,它不会在选择项目后第二次弹出列表(如Mark所述),并且在输入框已经有文本时也有效:

jQuery自动完成UI-我希望它能在无需用户键入任何内容的情况下启动焦点搜索

以下是我的完整解决方案(它还做了一些其他事情):

$.fn.ajaxselect = function(options) {
    var settings = {
        delay: 300,
        sourceData: function(term) {
            return {term:term};
        },
        sourceUrl: null,
        select: function(item) {},
        html: true,
        minLength: 0,
        autoSelect: true,
        autoFocus: true,
        showOnClick: null
    };

    if(options) $.extend(settings, options);
    if(settings.showOnClick === null) settings.showOnClick = settings.minLength === 0;

    $(this).autocomplete({
        source: function(request, response) {
            var data = settings.sourceData.call(this.element[0], request.term);
            if(data === false) {
                response([]);
                return;
            }
            $.ajax({
                url: settings.sourceUrl,
                dataType:  json ,
                data: data,
                success: function(data, textStatus, $xhr) {
                    response(data);
                },
                error: function($xhr, textStatus) {
                    response([]);
                }
            });
        },
        focus: function(e, ui) {
            return false; // don t fill input with highlighted value
        },
        search: function(e, ui) {
            if(settings.minLength < 0 && e.hasOwnProperty( originalEvent )) return false;  // don t search on keypress if minLength < 0 (use with showOnClick)
            $(this).data( lastSearch , this.value);
            return true;
        },
        select: function(e, ui) {
            if(!settings.autoSelect && e.keyCode === 9) return false; // don t select highlighted item on tab unless autoSelect is enabled
            if($(this).val() === $(this).data( lastSearch )) {
                if(settings.select.call(this, ui.item) !== false) {
                    $(this).val(ui.item.value);
                }
                $(this).data( selectedValue ,$(this).val()).trigger( change );
            } 
            return false;
        },
        open: function(e, ui) {
            $(this).data( isOpen , true);
        },
        close: function(e, ui) {
            $(this).data( isOpen , false);
        },
        minLength: settings.minLength,
        autoFocus: settings.autoFocus,
        delay: settings.delay,
        html: settings.html
    }).bind( change.ajaxselect , function() {
        $(this).toggleClass( ajax-selected , $(this).val() === $(this).data( selectedValue ));
    });

    if(settings.autoSelect) {
        $(this).bind( autocompletechange.ajaxselect , function(event, ui) {
            if($(this).val() !== $(this).data( selectedValue ) && this.value.length > 0) {
                var self = this;
                var data = $.extend({autoSelect:1},settings.sourceData.call(this, this.value));
                $(this).addClass( .ui-autocomplete-loading );
                $.ajax({
                    url: settings.sourceUrl,
                    dataType:  json ,
                    data: data,
                    success: function(data, textStatus, $xhr) {
                        if(data.length >= 1) {
                            var item = $.ui.autocomplete.prototype._normalize(data)[0];
                            if(settings.select.call(self, item) !== false) {
                                $(self).val(item.value);
                            }
                            $(self).data( selectedValue ,$(self).val()).trigger( change );
                        }
                    },
                    complete: function($xhr, textStatus) {
                        $(self).removeClass( .ui-autocomplete-loading );
                    }
                });
            }
        });
    }

    if(settings.showOnClick) {
        $(this).bind( click.ajaxselect , function(e) {
            if(!$(this).data( clickHandled ) && !$(this).data( isOpen )) {
                $(this).data( clickHandled ,true);
                $(this).autocomplete( search ,  );
            } else {
                $(this).data( clickHandled ,false);
            }
        }).bind( focus.ajaxselect , function(e) {
            if(!$(this).data( clickHandled ) && e.hasOwnProperty( originalEvent ) && $(this).val() === this.defaultValue && !$(this).data( isOpen )) {
                $(this).data( clickHandled ,true);
                $(this).autocomplete( search ,  );
            } else {
                $(this).data( clickHandled ,false);
            }
        })
    }

    return $(this);
};




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签