当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript for addEventListener not assigning correctly

问题描述:

This question already has an answer here:

  • JavaScript closure inside loops – simple practical example

    29 answers

网友答案:

try this:

document.getElementById(sel.id).addEventListener("change", function () {
        resetValues(this.id);
    });

by the time the event happens sel.id will be always 6. You need to address the element you added the listener to.

网友答案:

Try using resetValues(this.id); instead of resetValues(sel.id);

I think when you call resetValues(sel.id); here sel variable is sending reference to the function. And when you change value of sel it will change all value of previous sels

分享给朋友:
您可能感兴趣的文章:
随机阅读: