0

I have problem in sending one variable (i) to my php. My variable i is 6 everytime, how can i fix it?

$(document).ready(function(){
for (i=1; i<=5; i++){
    $('#rate'+ i +'_').click(function(){
    sendValue($(this).val(),i);
    });
}
});
function sendValue(str,str2){
$.post("/php/test.php",{ sendValue: str, sendValue2 : str2 },
    function(data){
    $('#display').html(data.returnValue);
    }, "json");
}  
Wisp Ever
  • 35
  • 6

1 Answers1

1

Pass the i as data to the handler of the click event

for (i=1; i<=5; i++){
    $('#rate'+ i +'_').click(i, function(e){
    sendValue($(this).val(),e.data);
    });
}
Musa
  • 96,336
  • 17
  • 118
  • 137
  • Please see http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example for an explanation of what's going on. – Jeff-Meadows Feb 05 '13 at 02:23