是否可以触发/绑定一个jQuery UI事件? [英] Is it possible to trigger / bind a jQuery UI event?

查看:155
本文介绍了是否可以触发/绑定一个jQuery UI事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

jQuery AutoComplete触发器更改事件


v1.9,我已成功实施 jQuery UI自动完成窗口小部件。我想知道是否可以触发/绑定一个jQuery UI事件,如果是这样,如何做到这一点。例如,如果我有

  $('#autocomplete_field%>')。autocomplete $ b change:function(event,ui){
// Make something ...
},
minLength:2,
...
}

可以运行jQuery UI change


em>注意:我尝试了 $($#

  $('#autocomplete_field'事件){
$('#autocomplete_field')。autocomplete('option','change')。call();
});
pre>

但我收到以下错误(从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 ui){
//当用户从下拉列表中选择时要做的事情
}
});



$(document).on('keyup,keydown,change','.selectorClass',function(event){
// I'm不确定你需要什么ui正确
var pos = $(this).position(); //获取.selectorClass的位置
var val = $(this).val(); // gets当前值.selectorClass
var width = $(this).width(); //获取宽度
//如果有多个'.selectorClass' - $(this)
});

这是否能让您了解如何使用$(this)?



有点冗余,可以处理同一文本框中的两个事件,但是如果这是你想要的 - 这将有一个功能自动完成,另一个地方,每次文本框是改变,无论如何已更改。


Possible Duplicate:
jQuery AutoComplete Trigger Change Event

I am using jQuery UI v1.9 and I successfully implemented the jQuery UI Autocomplete widget. I would like to know if it is possible to trigger / bind a jQuery UI event and, if so, how to make that. That is, for example, if I have

$('#autocomplete_field" %>').autocomplete({
  change: function (event, ui) {
    // Make something...
  },
  minLength: 2,
  ...
});

is it possible to run the jQuery UI change event by binding that to a keypress event?


Note: I tried

$('#autocomplete_field').bind('keypress', function(event) {
    $('#autocomplete_field').autocomplete('option', 'change').call();
});

but I get the following error (from the Firebug console):

TypeError: elem.nodeName is undefined
...ks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase(...

解决方案

this will cover autocomplete for the textbox:

  $(".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
   });

Does that give you an idea of how $(this) is used ?

a little redundant to handle events in 2 spots for same textbox , but if that's what you want - this will have a functioning autocomplete , and another place for handeling everytime the textbox is change no matter how it was changed.

这篇关于是否可以触发/绑定一个jQuery UI事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆