jQuery类选择器仍然在removeClass之后触发 [英] JQuery Class Selector still firing after removeClass

查看:204
本文介绍了jQuery类选择器仍然在removeClass之后触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个投票机制,通过AJAX发送一个快速的数据库时间戳。



一系列带有投票类型的按钮是投票的触发器,而有以下文字显示该特定项目有多少投票。



从点击事件运行AJAX方法后,我删除投票类,使得该项目不能超过一个。然而,我的问题是,即使删除了类,触发器仍然可以触发并增加投票数。



这是元素的HTML:

 < div class =points> 
< img class =votesrc =images / up.gifalt ='。$ idea ['id']。'>
< p class =vote - '。$ idea ['id']。'>'。$ points。'Points< / p>
< / div>

这是jQuery调用:



<$ p $点击(function(){
var iID = $(this).attr('alt');
var typeString =id =+ iID;
$ .ajax({
type:POST,
url:vote.php,
data:typeString,
success: function(txt){
$('。vote - '+ iID).html('< p>'+ txt +'Points< / p>');
}
}) ;
$(this).attr('src','images / voted.gif');
$(this).removeClass('vote');
});


解决方案

class 被用于引用它,而不是改变它的事件绑定行为。



要删除DOM元素点击事件使用 unbind


I am creating a little voting mechanism that sends a quick database timestamp through AJAX.

A series of buttons with the class "vote" are the triggers to vote, while there is text below to show how many "votes" are for that particular item.

After I run the AJAX method from the click event, I remove the "vote" class such that there cannot be more than one from that item. However my problem is that even with the class removed the trigger can still fire and increment as many votes.

Here is the HTML of the element:

        <div class="points">
        <img class="vote" src="images/up.gif" alt="'.$idea['id'].'">
        <p class="vote-'.$idea['id'].'">'.$points.' Points</p>
    </div>

Here's the jQuery Call:

    $('.vote').click(function(){
        var iID = $(this).attr('alt');
        var typeString = "id="+iID;
        $.ajax({
            type: "POST",
            url:"vote.php",
            data: typeString,
            success: function (txt){
                $('.vote-'+iID).html('<p>'+txt+' Points</p>');
            }
        }); 
        $(this).attr('src', 'images/voted.gif');
        $(this).removeClass('vote');
    });

解决方案

The class of a DOM element is used to reference it, not to alter its Event binding behavior.

To remove a DOM elements click event use unbind.

这篇关于jQuery类选择器仍然在removeClass之后触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆