2009-08-18 106 views
1

我正在使用來自YUI的自動完成小部件來實現實時搜索,如示例中所示。但是,當輸入搜索文本時,它可以正常工作,但在將文本粘貼到字段時無法工作。這將是在paste上啓動自動完成的正確方法?在文檔中找不到任何內容...YUI自動完成:粘貼後搜索?

編輯:粘貼不是Ctrl-V,它通常是上下文菜單中的「粘貼」。 YUI確實會對按鍵做出反應,但如果粘貼的鼠標,則不會發生任何反應。

回答

0

也許在關鍵事件中,您可以檢測到按住ctrl時是否按下了v。如果他們有,那麼做一個sendQuery('query ='+ textInput.value);

0

編輯

這裏是一個兼容性表,顯示該瀏覽器讓你訂閱粘貼事件。

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

這是他的測試頁面,在這裏你可以看到如何訂閱事件。

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

我想你可以訂閱,使用YUI &然後只是有回調是這樣的:

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

留意瀏覽器不兼容,貌似有些有一個奇怪的執行活動。

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是自動完成構件。

+0

您也可以使用['YAHOO.lang.later'](http://developer.yahoo.com/yui/docs/YAHOO.lang.html)而不是'window.setTimeout'。 – 2010-11-06 08:00:34