3

重複の可能性:
jQueryオートコンプリートトリガー変更イベント

jQuery UI v1.9を使用していて、jQueryUIオートコンプリートウィジェットを正常に実装しました。jQuery UIイベントをトリガー/バインドできるかどうか、可能であれば、その方法を知りたいです。つまり、たとえば、私が持っている場合

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

jQuery UIchangeイベントをイベントにバインドして実行することは可能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(...
4

3 に答える 3

0

これは、テキストボックスのオートコンプリートをカバーします。

  $(".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)がどのように使用されているかがわかりますか?

同じテキストボックスの2つの場所でイベントを処理するには少し冗長ですが、それが必要な場合は、オートコンプリートが機能し、テキストボックスがどのように変更されてもテキストボックスが変更されるたびに処理するための別の場所があります。

于 2012-11-20T15:42:48.160 に答える
0

jQuery UIソースを簡単に確認した後、最初の推測ではほとんど正解でした(jQuery UIウィジェットを使用してからしばらく経ちました)。

autocompleteなどはjQuery.Widgetビルダーを使用します。ビルダーは、呼び出されたイベントの前にウィジェットの名前を付けます(またはウィジェットwidgetEventPrefixが提供されている場合)。

の場合、これは、、autocompleteなどを介してイベントをトリガーおよびバインドできる必要があることを意味します(残りのイベントはウィジェットのドキュメントページにあります) 。autocompletechangeautocompletecloseautocomplete

于 2012-11-20T15:18:59.567 に答える
0

オートコンプリートから関数を抽象化するだけで簡単になると思います。

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

$('#autocomplete_field').bind('keypress', function(event) {
    changeFunction();
});

function changeFunction() {
//make something
}
于 2012-11-20T15:19:34.630 に答える