ajax send request to php same value

家住魔仙堡 提交于 2020-01-07 06:52:06

问题


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");
}  

回答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);
    });
}


来源:https://stackoverflow.com/questions/14698872/ajax-send-request-to-php-same-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!