簡體   English   中英

.html無法使用live()jQuery

[英].html not working with live() Jquery

我正在嘗試使用live方法,並且該方法最初可以按預期運行,但是ajax的“成功”回調在該函數的后續運行中無法正常運行。

$(function () {
    $('.vote').live('click', function () {
        url = '".base_url()."post/vote';
        post_id = $(this).attr('id');

        $.ajax({
            url: url,
            type: 'POST',
            data: 'post_id=' + post_id,
            success: function (msg) {
                post = $('.num_vote' + post_id);
                vote = $('.votes' + post_id);
                $(vote).html(msg); // working only the first time
            }
        });
        return false;
    });
});

根據所使用的jQuery版本,您可能需要使用.on函數,因為.live()在1.7中已棄用,在1.9中已刪除。

$(function(){
    $('.vote').on( 'click', function() {
            url = '".base_url()."post/vote';
            post_id = $(this).attr('id');
            $.ajax({
                    url: url,
                    type: 'POST',
                    data: 'post_id=' + post_id,
                    success: function(msg) {                            
                        post = $('.num_vote' + post_id);
                        vote = $('.votes' + post_id);
                            $(vote).html(msg); // working only the first time

                    }
            });
            return false;
    });
});

暫無
暫無

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

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