jQuery事件附加到对象数组

我想将一个事件绑定到选择元素,其中每个选择元素都将其转换为一个jquery-selectbox元素(https://github.com/marcj/jquery-selectBox)


var selectBoxes = [];

var selectElements = [];

$('.select').each(function (index, element) {

    //Instantiates selectbox plugin (https://github.com/marcj/jquery-selectBox) 

    selectBoxes[index] = new SelectBox($(element), settings = {});

    selectElements[index] = selectBoxes[index].getSelectElement(); //gets the particular select element


    //Binding a close event to perform some tasks

    $(selectElements[index]).bind('close', function (el) { 

        //Doing something here

    });

});

添加一个插件:https ://plnkr.co/edit/L3dwtVl4rjn2BKBe43x5 ? p = preview


当我在选择框中选择一个选项时,上面绑定的jquery事件将触发那里存在的许多select元素。就我而言,我有3个选择元素,因此触发了三次。


慕标琳琳
浏览 142回答 2
2回答

月关宝盒

根据插件文档,您使此过程变得过于复杂尝试更换所有东西$('select').selectBox(/* options*/).close(function () {    alert($(this).val());});请注意,该插件似乎没有维护很长时间
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript