문제

for (key in this.mybutton)
       button = this.mybutton[key]

       $(button).click(function() {
            console.log("click:" + button)
       });

The result is always the name of the last button. How can I add multiple listeners in jQuery/javascript or erase the javascript reference button to a string in the for.

thanks in advance

도움이 되었습니까?

해결책

You need a closure new scope :

for (key in this.mybutton)
   (function(button) {
       $(button).click(function() {
            console.log("click:" + button)
       });
   })(this.mybutton[key]);
}

or just use this:

$(button).click(function() {
    console.log("click:" + this)
});

Concatenating a DOM element and a string in the console log doesn't seem like a very good idea ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top