I'm validating a field using OnKeyUp. Unfortunately it is not running when the user selects an autocomplete suggestion in his browser.
I would like to know if there is any way I can trigger my JavaScript when the user clicks in the browser in开发者_C百科put suggestions.
For IE, you can listen to onpropertychange
. For FF/Chrome, you can listen to oninput
. IE9 supports oninput if you are not in quirks mode, so you may need some kind of browser-sniffing or feature-detection code to prevent the event from firing twice.
Here is a working JSFiddle. You'll have to input some value and submit once for it to be in your browser's autocomplete history. Then you'll see what event it fires when you click on the autocomplete suggestion.
Another option is to disable autocomplete entirely for a given input field:
<input type="text" name="myfield" id="myfield" autocomplete="off" />
One options you could take would be triggering events using the custom event model by YUI http://developer.yahoo.com/yui/event/#customevent
By doing this you can explicitly fire/trigger the way you see fit
精彩评论