jquery event for when <option>s are added or removed from a <select>

自作多情 提交于 2019-12-23 09:04:52

问题


Is there a jquery event for when options are added or removed from a select control on an HTML page? I have tried .change() but it only seems to fire when I click on option elements. I.e. this doesn't work:

$(function() {
    $('#select').change(function() {
            alert('hello world');
    })
});

Thank you :).


回答1:


You need a plugin to check if there is HTML added or removed from the DOM. livequery is a plugin http://docs.jquery.com/Plugins/livequery




回答2:


When adding a new element to an existing dropdown, you should do a: $("#selector").trigger ("change"); to get the behaviour you want.




回答3:


I don't think the standard change() event will do this for you. What you will want to do is have a function that is fired when you add a new option to the select box. How are you currently adding options to this select box?



来源:https://stackoverflow.com/questions/8124919/jquery-event-for-when-options-are-added-or-removed-from-a-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!