新手在这里..
我有一个运行3次的js函数,因为我已经调用了3次函数。
var deleteFunc = function() {
$('.delete').on('click', function(){
// ajax code here...
});
};
(loadData = function(){
// this will be in a loop didn't place its because it too long
var content = '<tr><td>1</td><td>John</td><td><span class="delete">Delete</span></td></tr>';
$('#first_tbl').append(content);
deleteFunc();
})();
(loadSecondData = function(){
// this will be in a loop didn't place its because it too long
var content = '<tr><td>1</td><td>John</td><td><span class="delete">Delete</span></td></tr>';
$('#second_tbl').append(content);
deleteFunc();
})();
(loadThirdData = function(){
// this will be in a loop didn't place its because it too long
var content = '<tr><td>1</td><td>John</td><td><span class="delete">Delete</span></td></tr>';
$('#third_tbl').append(content);
deleteFunc();
})();
当我点击.delete
时,deleteFunc()
会运行3次。
我知道我会创建3个删除函数,例如deleteFunc()
deleteSecondFunc()
deleteThirdFunc()
,但我注意到它将是多余的,因为它具有相同的功能和代码。
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:2)
删除先前的绑定,然后添加新绑定。
var deleteFunc = function() {
$('.delete').off('click').on('click', function(){
// some ajax code here...
});
};