How do I bind to itemAdded event of a drop down using jQuery
I'm adding elements to a drop down using jQuery. Currently I'm adding new items using below which is working fine:
$("#treatmentGroupDropDown").append(new Option(tempGroupName, tempGroupIndex));
However I want to create and subscribe to the itemAdded event which I've tried using the code below, but this isn't working:
$("#treatmentGroupDropDown").bind('itemAdded', funct开发者_如何学JAVAion (event, item) { alert('item added: ' + item.toString()); });
Will something like this work?
You need some thing like this -
$("#treatmentGroupDropDown").bind('itemAdded', function(event, item) {
alert('item added: ' + $(item).val());
});
var option = new Option("Test", "Test")
$("#treatmentGroupDropDown").append(option);
$('#treatmentGroupDropDown').trigger('itemAdded', option);
This will trigger the 'itemAdded' event after the new option has been appended, passing the option object that has just been created to the function.
Working demo - http://jsfiddle.net/ipr101/MxD25/
The bind
function doesn't have any itemAdded
event.
Instead, you'd better add the function you want to trigger after the statement that appended your new Option.
精彩评论