1

クリックされたahrefに基づいてdivコンテンツをロードし、パラメーターを渡そうとしています。たとえば、リンク1リンク1をクリックすると、値「3」がprocess.phpに渡され、値「apple isgoodforyou」が返されます。

ただし、送信ボタンがないと値を渡すことができないようです。とにかく、パラメータを別のphpファイルに渡して処理し、値を返すことができますか?

$(document).ready(function(){
     $("#testing a").live("click", function(evt){
         var id= $(this).attr('id');
        $.post("process.php", { id: id },
        function(data) {
          alert(data);
                    $('#result').load(data);
        });
     })

});

以下は私のHTMLです

<div id="testing">
<a href="" id="11"> hello </a>
</div>

<div id="result"></div>

あなたの助けに感謝します、どうもありがとう!

4

1 に答える 1

3

値に数値を使用しないでくださいid。代わりに、文字を前に付けるかdata-、要素の属性に追加することを検討してください。

さらに、$.live()は非推奨であり、今後は$.on()イベントの委任に使用す​​ることをお勧めします。以下のコードでこれを処理しましたが、id問題は残ります。

最後に、$.load()$.html()は同じではありません。要素にロード する場合dataは、load メソッドを呼び出しません (名前が混乱を招く可能性がありますが)。

// Short-hand version of $(document).ready();
$(function(){
  // Handle anchor clicks on #testing
  $("#testing").on("click", "a", function(e){
    // Prevent links from sending us away
    e.preventDefault();
    // POST our anchor ID to process.php, and handle the response
    $.post("process.php", { 'id': $(this).attr("id") }, function(data){
      // Set the response as the HTML content of #result
      $("#result").html(data);
    });
  });
});

ファイルからprocess.php、次のようなものが得られる可能性があります。

$msg = array( 
    "...chirp chirp...",
    "This is response number 1",
    "And I am the second guy you'll see!",
    "Apples are good for you!" 
);

$num = $_POST["id"] || 0;

echo $msg[ $num ];
于 2012-05-22T01:20:52.807 に答える