开发者

jquery ui autocomplete - key events

I'd like to modify the enter key to fire off a window.open(ui.item.url)

Could someone help me do this? The autocomplete key code block is here:

.bind( "keydown.autocomplete", function( event ) {
if ( self.options.disabled ) {
    return;
}

var keyCode = $.ui.keyCode;
switch( event.keyCode ) {
case keyCode.PAGE_UP:
    self._move( "previousPage", event );
    break;
case keyCode.PAGE_DOWN:
    self._move( "nextPage", event );
    break;
case keyCode.UP:
    self._move( "previous", event );
    // prevent moving cursor to beginning of text field in some browsers
    event.preventDefault();
    break;
case keyCode.DOWN:
    self._move( "next", event );
    // prevent moving cursor to end 开发者_JS百科of text field in some browsers
    event.preventDefault();
    break;
case keyCode.ENTER:
case keyCode.NUMPAD_ENTER:
    // when menu is open or has focus
    if ( self.menu.element.is( ":visible" ) ) {
        event.preventDefault();
    }
    //passthrough - ENTER and TAB both select the current element
case keyCode.TAB:
    if ( !self.menu.active ) {
        return;
    }
    self.menu.select( event );
    break;
case keyCode.ESCAPE:
    self.element.val( self.term );
    self.close( event );
    break;
default:
    // keypress is triggered before the input value is changed
    clearTimeout( self.searching );
    self.searching = setTimeout(function() {
        // only search if the value has changed
        if ( self.term != self.element.val() ) {
            self.selectedItem = null;
            self.search( null, event );
        }
    }, self.options.delay );
    break;
}
})


I added a if block to the success method so that if the keypress == 13 I just open a new window with the url and return


I assume you're experiencing trouble with popup-blockers.

If that's the case, then you should know that browsers will block popups that weren't triggered directly by a user-interaction. Usually this means a mouse-click is required, although I'm not entirely certain whether an enter-key trigger will be blocked. You may want to open the window and then change the location of the window after the window is loaded.

var newWin = window.open(null, 'name');
newWin.onload = function(e)
{
  newWind.location = url;
}

you may need to set up an onReadyStateChange event for IE instead of the onload event.

jQuery has some issues when working in the context of a different window, so be certain to check what window reference you're using, else your global vars might disappear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜