Jestem trying to make jQuery kompatybilny kompozytowe wejście HTML.Dlaczego moje zdarzenie niestandardowe jQuery `change` jest wyzwalane dwukrotnie?
Po zmianie select
, alert
wywołuje się dwukrotnie. Czemu?
Biorąc prosty węzeł kontenera:
<span id="x"></span>
Oto demo w pełni robocze:
function el (type) {
return document .createElement (type);
}
function opt (sel, value) {
let node = el ('option');
node .value = value;
node .appendChild (document .createTextNode (value));
sel .appendChild (node);
}
let x = document .getElementById ('x');
let a = el ('select');
let b = el ('select');
x .appendChild (a);
x .appendChild (b);
opt (a, 'a_1');
opt (a, 'a_2');
opt (b, 'b_1');
opt (b, 'b_2');
$(a) .change (function() {$(x) .trigger ('change');});
$(b) .change (function() {$(x) .trigger ('change');});
$(x) .change (function() {alert ('changed');});
Ponieważ zarówno aib = wybrać? Czy starasz się pozwolić b = el ("wybierz # b") i pozwól a = el ("wybierz # a")? –
'el' tworzy nowy element. – spraff