使用 Jquery 检测选择列表的更改
有没有办法检测选择列表的值何时被 JavaScript 设置为空而不是被用户设置为空?看来更改事件只能通过鼠标或键盘触发。
有没有办法检测选择列表中的选项数量何时发生变化(添加、删除)?
Is there a way to detect when the value of a select list is set to empty by a javasscript and not by the user? It seems that the change-event only triggers by mouse or keyboard.
And is there a way to detect when the number of options in a select list changes (added, removed)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用 javascript 更改选择的值时,必须手动触发更改事件。例如:
在此示例中,值设置为 10,并触发更改事件。如果选择附加了一个事件处理程序,它将被执行。
You have to trigger the change event manually, when you are changing the value of a select with javascript. E.g:
In this example the value is set to 10 and the change event is triggered. If there is an event handler attached to the select, it will be executed.
$("#idofselect").change(function(){ } );
$("#idofselect").change(function(){ });
要回答您的第一个问题,不可能检测导致更改事件本身的选择列表中的更改的原因。
但是,如果有 javascript 代码更改选择列表,您可以在其中添加一些逻辑来执行此场景中所需的任务。
To answer your first question, not it's not possible to detect what caused the change in the select list in the change event itself.
However, if there is javascript code changing the select list you could add some logic in there to perform the tasks needed in this scenario.