Jquery bind of click causes each row in the list to bind one event

  • 2020-03-30 03:39:02
  • OfStack

It is common to click on a line in the list to pop up a request for details. When using the jquey bind click event, passers' lack of attention may result in each row being clicked showing the same content, mostly because of problems caused by passers' lack of attention. The simple code is as follows:


for(var i=0;i<2;i++) { 
$("#b" + i).bind("click", {'bindText':bindText + i}, function(e){ 
butClick(e); 
}); 
}

Test code:


<html> 
<head> 
<script type="text/javascript" src="/jquery/jquery.js"></script> 

<script type="text/javascript"> 
$(document).ready(function(){ 
var bindText = 'AAA'; 
for(var i=0;i<2;i++) { 
$("#b" + i).bind("click", {'bindText':bindText + i}, function(e){ 
butClick(e); 
}); 
} 
}); 

function butClick(e) { 
alert(e.data.bindText); 
} 
</script> 
</head> 
<body> 
<button id="b0"> Click here 0</button> 
<button id="b1"> Click here 1</button> 
</body> 
</html>

Related articles: