1

ユーザーがボタンをクリックするとすぐに、ゲームの名前をphp関数に渡そうとして、壁に頭をぶつけてきました。ユーザーがビデオゲーム名の値を持つボタンをクリックすると、phpスクリプトがそのゲームのランキングをチェックし、そのランキングをhtmlに返します。以前に投稿リクエストを機能させましたが、ここでも変数名をゲームの1つに手動で設定してテストすると、機能します。助けてください!

<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投稿を実行します

$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 に答える