简体   繁体   English

.html无法使用live()jQuery

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

I'm trying to use the method live and it initially works as intended, but the ajax 'success' callback is not functioning correctly on subsequent runs of the function. 我正在尝试使用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;
    });
});

Depending on the version of jQuery that you are using, you may need to use the .on function as .live() was deprecated in 1.7 and removed in 1.9. 根据所使用的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