繁体   English   中英

ajax防止在mouseenter上发布双重帖子

[英]ajax prevent double posting on mouseenter

有人可以在我的代码中指出我需要什么来阻止它在mouseenter上超链接标记时加倍数据。 我在那里加了一个标志,但仍然继续加倍。 我可能做错了可能有人看看我的代码并看到它有什么问题 - 看看你是否可以防止它在mouseenter上发布。 请告诉我你的变化 - 感谢KDM。

(function($){
    $.fn.rating_display = function() {
        var _this = this;
        var id = $(_this).data('id');
        var position = $(this).parent().position();         
            var left = position.left - 15;  
            var top  = position.top + $(this).height() + 13;
        var isLoading = false;

        function clear_ratings() {
            $('.ratings-content').html(""); 
        }

        $(document).on('click', function(e) {
            var element = e.target;

            /*else if($(element).closest('.rating').length){
                $('.ratings-display').show();
            }*/

        });
        // here is where I'm having trouble with double posting
        $(this).on('mouseenter click', function(e) {
            if(isLoading == true) return false;

            $.ajax({
                type:'POST', 
                dataType:"html",
                data:{product_id:id},
                url:"../../webservices/get_rating.php",
                beforeSend: function() {
                    clear_ratings();
                    $('.ratings-display').show().css({'left':left + 'px', 'top':top + 'px'});
                    isLoading = true;   
                },
                success: function(data) {                                                                       
                    $('.ratings-content').append(data);                                             
                }, error:function(data, status, xhr) {
                    clear_ratings();
                    $('.ratings-content').html(data + "\r\n" + status + "\r\n" + xhr);  
                }
            });
        }).on('mouseleave', function(e) {
            var target = e.relatedTarget;

            if($(target).closest('.ratings-display').length) {
                return false;   
            }else{
                $('.ratings-display').hide();
                clear_ratings();
                isLoading = false;
            }
        });

        $('.ratings-display').on('mouseleave',function (e) {            
            var target = e.relatedTarget;

            if($(target).closest('.rating').length) return false;

            if(!$(target).closest('.ratings-display').length) {
                $('.ratings-display').hide();
                clear_ratings();isLoading = false;  
            }


        });
    }
})(jQuery);

'mouseenter click'表示操作在mouseenter执行一次,如果单击则再次执行。

尝试设置isLoading = true; 在ajax调用之前而不是在beforesend函数之前。 并且您还希望在ajax调用完成时重置isLoading = false ,而不是在mouseleave上重置。 根据您是否出于键盘导航原因,您还可以完全停止收听点击事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM