$('[name="properties_box"]').change(function() {
if(this.checked) {
console.log(properties.length - 1);
for (var i = 0; i < (properties.length - 1); i++) {
(function(index) {
console.log('iterator: ' + index);
//do ajax here
})(i);
}
}
});
它应该只记录0-7。但是这样做了三次。
7 main.js?ver=20130905:732
iterator: 0 main.js?ver=20130905:737
iterator: 1 main.js?ver=20130905:737
iterator: 2 main.js?ver=20130905:737
iterator: 3 main.js?ver=20130905:737
iterator: 4 main.js?ver=20130905:737
iterator: 5 main.js?ver=20130905:737
iterator: 6 main.js?ver=20130905:737
7 main.js?ver=20130905:732
iterator: 0 main.js?ver=20130905:737
iterator: 1 main.js?ver=20130905:737
iterator: 2 main.js?ver=20130905:737
iterator: 3 main.js?ver=20130905:737
iterator: 4 main.js?ver=20130905:737
iterator: 5 main.js?ver=20130905:737
iterator: 6 main.js?ver=20130905:737
7 main.js?ver=20130905:732
iterator: 0 main.js?ver=20130905:737
iterator: 1 main.js?ver=20130905:737
iterator: 2 main.js?ver=20130905:737
iterator: 3 main.js?ver=20130905:737
iterator: 4 main.js?ver=20130905:737
iterator: 5 main.js?ver=20130905:737
iterator: 6
有人可以解释为什么以及如何解决它?我将在其中进行一些AJAX调用,因此重要的是它不会运行超过需要。
答案 0 :(得分:0)
我认为绑定更改三次的问题, 提到发表的评论我建议将其作为答案
$('[name="properties_box"]').unbind()
在绑定事件之前调用use代码。