如何使用jQuery绑定到下拉列表的itemAdded事件

如何使用jQuery绑定到下拉列表的itemAdded事件,jquery,Jquery,我正在使用jQuery向下拉列表中添加元素。目前,我正在使用以下功能添加新项目,效果良好: $("#treatmentGroupDropDown").append(new Option(tempGroupName, tempGroupIndex)); 但是,我想创建并订阅itemAdded事件,我已尝试使用下面的代码创建并订阅该事件,但这不起作用: $("#treatmentGroupDropDown").bind('itemAdded', function (event, item) { a

我正在使用jQuery向下拉列表中添加元素。目前,我正在使用以下功能添加新项目,效果良好:

$("#treatmentGroupDropDown").append(new Option(tempGroupName, tempGroupIndex));
但是,我想创建并订阅itemAdded事件,我已尝试使用下面的代码创建并订阅该事件,但这不起作用:

$("#treatmentGroupDropDown").bind('itemAdded', function (event, item) { alert('item added: ' + item.toString()); });
类似于此的操作是否有效?

该函数没有任何添加的
事件


相反,您最好在附加新选项的语句之后添加要触发的函数。

您需要这样的东西-

$("#treatmentGroupDropDown").bind('itemAdded', function(event, item) {
    alert('item added: ' + $(item).val());
});

var option = new Option("Test", "Test")
$("#treatmentGroupDropDown").append(option);
$('#treatmentGroupDropDown').trigger('itemAdded', option);
这将在追加新选项后触发“itemAdded”事件,将刚刚创建的选项对象传递给函数

工作演示-