问题
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