查找页面上的所有剑道下拉菜单

Jes*_*rty 1 jquery kendo-ui

我想创建可重用的代码来查找我的页面上的每个kendoDropDown并在其上设置一个事件处理程序.这里是找到单个kendoDrop的代码并执行我想要的操作:

       var cb = $("#myID").data("kendoDropDownList");
       console.log("cb: " + cb);
       if (cb)
            cb.close();
Run Code Online (Sandbox Code Playgroud)

我需要的是在页面上找到每个kendoDropDown的代码,并为每个代码添加相同的事件处理程序.我试过了

        $("input").each(function (index, element) {
           if (element) {
               var cb = element.data("kendoDropDownList");
               if (cb) {
               console.log("element: " + element);
               element.close();
               }
           }
       });
Run Code Online (Sandbox Code Playgroud)

但显然我错过了一些东西.

Lar*_*ner 7

element参数是一个DOM对象,而不是一个jQuery对象,所以你必须再次包装它; 此外,您需要调用cb.close()而不是element.close()(或绑定到cb,因为您要添加事件处理程序).您也可以简单地引用this而不是element参数:

var handler = function (e) {
    console.log("open");
};

$("input").each(function () {
    var cb = $(this).data("kendoDropDownList");
    if (cb) {
        // attach handler to cb
        cb.bind("open", handler)
    }
});
Run Code Online (Sandbox Code Playgroud)