简体   繁体   English

YUI自动完成:粘贴后搜索?

[英]YUI Autocomplete: search after pasting?

I'm using an auto-complete widget from YUI to implement live search as in the examples. 我正在使用来自YUI的自动完成小部件来实现实时搜索,如示例中所示。 However, it works fine when search text is typed in, but fails to work when the text is pasted into the field. 但是,当键入搜索文本时,它可以正常工作,但是当文本粘贴到字段中时,它将无法工作。 Which would be the proper way to initiate an autocompletion on paste? 哪种方法可以对粘贴启动自动补全? Haven't found anything for that in the documentation... 尚未在文档中找到任何相关内容...

EDIT: Pasting is not Ctrl-V, it's usually "Paste" from the context-menu. 编辑:粘贴不是Ctrl-V,通常是上下文菜单中的“粘贴”。 YUI does react to a keypress, but doesn't if anything is pasted by the mouse . YUI确实对按键有反应,但是如果鼠标粘贴任何内容,则不会。

We have extended YUI's autocomplete widget and handle paste from the context menu in this way: 我们扩展了YUI的自动完成小部件,并以这种方式处理上下文菜单中的粘贴:

YAHOO.util.Event.on(input, 'paste', function(e, autocomplete) {
    // We're interested in the value of the input field after text is pasted into
    // it instead of the pasted text because the autocomplete proposals are based 
    // upon the field's whole value. The paste event happens before the input 
    // field has been updated so we need to wait until after this event has been 
    // handled to check the value of the input field.
    window.setTimeout(function() {
        if (autocomplete._sInitInputValue !== autocomplete.getInputEl().value) {
            autocomplete.sendQuery(autocomplete.getInputEl().value);
        }
    }, 1);
}, this);

Where this is the autocomplete widget. this是自动完成小部件。

Perhaps on key event, you could detect if they pressed v while holding ctrl. 也许在按键事件上,您可以检测到按住ctrl时是否按下v。 If they have, then do a sendQuery('query=' + textInput.value); 如果有,则执行sendQuery('query ='+ textInput.value);

Edit 编辑

Here's a compatibility table showing which browsers let you subscribe to the paste events. 这是一个兼容性表,其中显示了允许您订阅粘贴事件的浏览器。

http://www.quirksmode.org/dom/events/cutcopypaste.html http://www.quirksmode.org/dom/events/cutcopypaste.html

Here's his testing page where you can see how to subscribe to the events. 这是他的测试页,您可以在其中查看如何订阅事件。

http://www.quirksmode.org/dom/events/tests/cutcopypaste.html http://www.quirksmode.org/dom/events/tests/cutcopypaste.html

I imagine you could subscribe to that using YUI & then just have your callback be this: 我想您可以使用YUI订阅它,然后让您的回调如下:

function() { 
    autoCompleteObject.sendQuery(autoCompleteElement.value);
}

Watch out for browser incompatibilities, looks like some have a weird implementation of the events. 当心浏览器的不兼容性,看起来有些事件的实现很怪异。

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

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