1

一旦用户单击按钮,我一直在试图将游戏名称传递给我的 php 函数。这个想法是用户单击一个按钮,该按钮具有其视频游戏名称的值,然后 php 脚本检查该游戏的排名并将该排名返回到 html。我之前已经使发布请求工作,即使在这里我手动将变量名称设置为其中一个游戏来测试它,它也可以工作。请帮忙!

<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>
4

2 回答 2

1

你应该把它$.post放到你的点击处理程序中......所以它只会在你真正点击按钮时运行......现在你的代码设置了一个n变量,它的值是一个空的jQuery对象(为什么?)。然后它在按钮上附加一个点击处理程序。然后它运行一个$.post请求——n仍然是一个空的 jQuery 对象。单击按钮会在很久以后发生......

此外,应避免使用全局变量。var声明变量时应使用关键字。

于 2013-03-11T23:16:34.363 回答
1

只需当用户单击按钮时。在点击处理程序中,获取值并执行 http post

$ajax 或 $POST

例如 -

$(document).ready(function() {
      $('#target').click(function()
            $.ajax({  
              type: "POST",
              url: "url...",
              data: "n="+nVal+",
              success: function(html){
                alert( "Submitted");
              }
           });
      });
});
于 2013-03-11T23:51:12.707 回答