简体   繁体   English

更改事件触发jquery处理程序但不触发javascript处理程序

[英]Change event fires jquery handler but not javascript handler

I am seeing a weird behavior that when I use addEventListener , change event handler doesn't get fired on the select with display: none; 我看到一个奇怪的行为,当我使用addEventListenerchange事件处理程序不会被选中使用display: none; . On the other hand, when I use jQuery, it does fire. 另一方面,当我使用jQuery时,它确实会触发。 What's the trick that allows jQuery to do that and how do I make it work in plain JavaScript? 允许jQuery执行此操作的技巧是什么?如何使其在纯JavaScript中工作?

Here is code that shows the example. 这是显示示例的代码。

 // This doesn't work document.getElementById("notDisplayedSelect").addEventListener("change", function(e) { $("#output").text("When select is hidden events still fire: new value = " + $(this).val()); }); /* // This works $("#notDisplayedSelect").change(function (e) { $("#output").text("When select is hidden events still fire: new value = "+ $( this ).val()); }); */ $("#setValue").click(function() { $("#notDisplayedSelect").val("3").trigger("change"); }); 
 #notDisplayedSelect { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <select id="notDisplayedSelect"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> <span id="output">I show selected value</span> <button id="setValue">Select third option in the hidden select</button> 

From jQuery trigger() docs 来自jQuery trigger() docs

Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. 当相应的事件发生时,将触发附加.on()或其快捷方法之一的任何事件处理程序。

When you do 当你这样做

 $("#notDisplayedSelect").val("3").trigger("change");

only handlers attached with on() or change() will be triggered. 只触发附加on()change()处理程序。

Apparently, this does not include handlers attached with the native 显然,这不包括与本机附加的处理程序

addEventListener("change", function (e) {...});

If you want to trigger the handler attached by native javascript, you can do 如果你想触发本机javascript附加的处理程序,你可以这样做

var event = new CustomEvent("change");
document.getElementById("notDisplayedSelect").dispatchEvent(event);

Update Workaround for above in IE 在IE中更新上面的解决方法

var event = document.createEvent("HTMLEvents");
event.initEvent("change", true, true);
document.getElementById("notDisplayedSelect").dispatchEvent(event);

DEMO DEMO

See How to trigger event in JavaScript? 请参阅如何在JavaScript中触发事件?

When you add a handler using jQuery, jQuery keeps information about this handler in its internal memory, in addition to adding it as a browser event listener. 当您使用jQuery添加处理程序时,除了将其添加为浏览器事件侦听器之外,jQuery还将有关此处理程序的信息保存在其内部内存中。 Then when you call trigger("change") , it just calls these handlers itself, rather than going through the browser. 然后当你调用trigger("change") ,它只调用这些处理程序,而不是通过浏览器。

In the browser, it makes no sense to change an invisible element, because the change event is associated with user interaction. 在浏览器中,更改不可见元素没有意义,因为change事件与用户交互相关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM