簡體   English   中英

Ajax通話后如何更新div標簽?

[英]How do i update a div tag after a ajax call?

我有帶有多個按鈕的腳本,單擊這些按鈕可通過AJAX運行php腳本。 我現在希望結果通過按鈕顯示在div中。 我已經嘗試過這個和父母,但沒有工作。

下面的示例:單擊.showme我希望結果在#here div中顯示。

 $(document).ready(function() { $('.showme').bind('click', function() { var id = $(this).attr("id"); var num = $(this).attr("class"); var poststr = "request=" + num + "&moreinfo=" + id; $.ajax({ url: "../../assets/php/testme.php", cache: 0, data: poststr, success: function(result) { $(this).getElementById("here").innerHTML = result; } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='request_1 showme' id='rating_1'>More stuff 1 <div id="here"></div> </div> <div class='request_2 showme' id='rating_2'>More stuff 2 <div id="here"></div> </div> <div class='request_3 showme' id='rating_3'>More stuff 3 <div id="here"></div> </div> 

我認為這可以做到:

$(document).ready(function () {
    $('.showme').bind('click', function () {

        //keep the element reference in a variable to use in ajax success
        var _this = $(this);

        var id = $(this).attr("id");
        var num = $(this).attr("class");
        var poststr = "request=" + num + "&moreinfo=" + id;
        $.ajax({
            url: "../../assets/php/testme.php",
            cache: 0,
            data: poststr,
            success: function (result) {
                //use the element reference you kept in variable before ajax call instead of $(this)
                //also use jQuery style, getElementById is undefined if you call after $(this)
                //.find() will call it's child in any level, also better you use classes instead of using same id multiple times
                _this.find("#here").html(result);
            }
        });
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM