jQuery's click function is executed multiple times

In use, as follows:

var confirm = $("#"+confirmButtonID);
var cancel = $("#"+cancelButtonID);
confirm.click(function(){
    
    
	console.log("confirm del btn click.");
	// 取消按钮的事件: 1. cover隐藏; 2. 该pannel的隐藏;
	$("#"+coverID).hide("normal");
	$("#"+pannelID).hide("normal");
	delbtncallback(obj);
});
cancel.click(function(){
    
    
	// 取消按钮的事件: 1. cover隐藏; 2. 该pannel的隐藏;
	$("#"+coverID).hide("normal");
	$("#"+pannelID).hide("normal");
});

When it is executed multiple times, through the console.log()test, it is found that it will not be like the ideal coverage clickmethod, so it will be executed multiple times.
Baidu checked it and found that it can be solved by simple execution bindand unbindsolution, link address: here

$(!!!).bind('click',function(){
    
    
 $(!!!).unbind('click');
});

After the modification, it was tested and sure enough! code show as below:

var confirm = $("#"+confirmButtonID);
var cancel = $("#"+cancelButtonID);
confirm.bind("click", function(){
    
    
	console.log("confirm del btn click.");
	// 取消按钮的事件: 1. cover隐藏; 2. 该pannel的隐藏;
	$("#"+coverID).hide("normal");
	$("#"+pannelID).hide("normal");
	delbtncallback(obj);
	confirm.unbind("click");
});
cancel.bind("click", function(){
    
    
	// 取消按钮的事件: 1. cover隐藏; 2. 该pannel的隐藏;
	$("#"+coverID).hide("normal");
	$("#"+pannelID).hide("normal");
	cancel.unbind("click");
});

Guess you like

Origin blog.csdn.net/qq_26460841/article/details/113788310