Possible Duplicate:
jQuery AutoComplete Trigger Change Event
我正在使用 jQuery UI v1.9,并且成功实现了 jQuery UI Autocomplete小部件。我想知道是否可以触发/绑定(bind) jQuery UI 事件,如果可以,如何实现。也就是说,例如,如果我有
$('#autocomplete_field" %>').autocomplete({
change: function (event, ui) {
// Make something...
},
minLength: 2,
...
});
是否可以运行 jQuery UI change
事件通过将其绑定(bind)到 keypress
事件?
注意:我尝试过
$('#autocomplete_field').bind('keypress', function(event) {
$('#autocomplete_field').autocomplete('option', 'change').call();
});
但我收到以下错误(来自 Firebug 控制台):
TypeError: elem.nodeName is undefined
...ks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase(...
最佳答案
这将覆盖文本框的自动完成:
$(".autoCompleteTxtBox").autocomplete({
source: "www.url.com/Source",
select: function (event, ui) {
//something to do when user selects from dropdown
}
});
$(document).on('keyup, keydown, change', '.selectorClass', function(event){
//I'm not sure what you need ui for exactly
var pos = $(this).position(); // gets position of .selectorClass
var val = $(this).val(); // gets current value of .selectorClass
var width = $(this).width(); // gets width
// if there are multiple '.selectorClass' - $(this) is the one that fired event
});
这是否让您了解 $(this) 的用法?
在同一个文本框的两个位置处理事件有点多余,但如果这就是你想要的 - 这将有一个功能自动完成功能,以及每次文本框更改时处理的另一个位置,无论它如何更改。
关于javascript - 是否可以触发/绑定(bind) jQuery UI 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13476146/