1

我正在使用来自 YUI 的自动完成小部件来实现实时搜索,如示例中所示。但是,在输入搜索文本时它可以正常工作,但在将文本粘贴到字段中时无法正常工作。哪个是启动粘贴自动完成的正确方法?在文档中没有找到任何相关内容...

编辑:粘贴不是 Ctrl-V,它通常是上下文菜单中的“粘贴”。YUI 确实对按键做出反应,但如果鼠标粘贴了任何内容,则不会。

4

3 回答 3

3

我们以这种方式扩展了 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);

this自动完成小部件在哪里。

于 2010-06-15T04:59:04.340 回答
0

编辑

这是一个兼容性表,显示了哪些浏览器允许您订阅粘贴事件。

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

这是他的测试页面,您可以在其中查看如何订阅事件。

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

我想你可以使用 YUI 订阅它,然后让你的回调是这样的:

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

注意浏览器不兼容,看起来有些事件的实现很奇怪。

于 2009-08-18T16:26:55.610 回答
0

也许在关键事件上,您可以检测到他们是否在按住 ctrl 时按下了 v。如果有,则执行 sendQuery('query=' + textInput.value);

于 2009-08-18T14:20:37.623 回答