只要用戶點擊一個按鈕,我一直在撞牆,試圖將遊戲名稱傳遞給我的PHP函數。這個想法是用戶點擊一個按鈕,該按鈕具有其視頻遊戲名稱的值,然後php腳本檢查該遊戲的排名並將該排名返回給html。我之前提出了發佈請求,甚至在這裏我手動將變量名稱設置爲其中一個遊戲來測試它時,它就可以工作。請幫忙!如何讓我的jquery post請求在點擊時使用從函數中存儲的變量?
<script>
$(document).ready(function(){
//global n variable
n = $();
$('#target').click(function(){
//set value of n to the game's name
n = $(this).val();
});
//then send the name as term to my php function to deal with it and return
//the ranking
$.post('getTotal.php', {term: n}, function(data){
//been trying to see if anything comes through for debugging purposes
alert(data);
//commented this out for now, just puts it in the div I left open
//$('#total').html(data);
});
});
</script>