要素で .trigger() イベントを使用すると、このイベントにバインドされた他のイベントが発生しません。この行にコメントすると、すべてがうまくいきます。私は django-autocomplete-light ライブラリを使用しているので、この例を単純化しました。
<input type="text" class="js-input">
<div class="js-box">
<!-- code inside the box is loaded dynamically -->
<a href="#" class="js-autocomplete-item"></a>
</div>
動作していません
selectChoice イベントが発生します。
var boxClickHandler = function(e) {
var current = this.box.find('.' + this.hilightClass);
$(".js-input").trigger('selectChoice', [current, this]);
};
$(".js-box").on("mouseclick", ".js-autocomplete-item", $.proxy(boxClickHandler, this));
// somewhere else in the code
$(".js-input").bind('selectChoice', function(e, choice, autocomplete) {
// empty handler
});
// somewhere else in the code
$(document).on("click", ".js-autocomplete-item", function(e){
// FIXME: this code is not running
});
働く
var boxClickHandler = function(e) {
var current = this.box.find('.' + this.hilightClass);
// $(".js-input").trigger('selectChoice', [current, this]);
};
$(".js-box").on("mouseclick", ".js-autocomplete-item", $.proxy(boxClickHandler, this));
// somewhere else in the code
$(".js-input").bind('selectChoice', function(e, choice, autocomplete) {
// empty handler
});
// somewhere else in the code
$(document).on("click", ".js-autocomplete-item", function(e){
// when selectChoice event is not triggered from boxClickHandler this code is running
});
私は少し混乱しています。私は多くのアプローチを試しましたが、これは単純なものだと思います。なぜなら、このコードは少し前に実行されていたからです (Django 1.8 を実行するために、このコードと他の多くのコードを使用する django-autocomplete-light lib のアップグレードを行う前)。しかし、それはjQueryと関係があるだけのようです。誰かが同様の問題を抱えていましたか?