【问题标题】:How to disable/enable a button/link with jQuery?如何使用 jQuery 禁用/启用按钮/链接?
【发布时间】:2012-02-01 19:23:08
【问题描述】:

一旦您单击赞成投票,它就会启用,它只能通过单击反对票来禁用。如何做到这一点,如果我单击启用的按钮,它将被禁用。

$("a.vote_down").click(function() {
    //get the id
    the_id = $(this).attr('id');

    //the main ajax request
    $.ajax({
        type: "POST",
        data: "action=vote_down&id="+$(this).attr("id"),
        url: "votes.php",
        success: function(msg) {
            $(".vote_down").addClass("active");
            $(".vote_up").removeClass("active");
            $("span#votes_count").html(msg);
        }
    });
});

其中一个链接:

<a href='javascript:;' class='vote_up<?php if($liked["points"]=="1") echo " active";?>' id='<?php echo $id; ?>'>Vote Up</a>

【问题讨论】:

    标签: jquery select jquery-selectors


    【解决方案1】:

    尝试将其用作active 类的实时点击事件。添加了一些抽象。

    $("a.vote_down.active").live("click", function(e) {
        Vote($(this).attr('id'), false);
    });
    
    $("a.vote_up.active").live("click", function(e) {
        Vote($(this).attr('id'), true);
    });
    
    function Vote(id, value) {
        //the main ajax request
        var action = value ? "vote_up" : "vote_down"
        $.ajax({
            type: "POST",
            data: "action=" + action + "&id=" + id,
            url: "votes.php",
            success: function(msg) {
                $(".vote_down").toggleClass("active", value);
                $(".vote_up").toggleClass("active", !value);
                $("span#votes_count").html(msg);
            }
        });
    }
    

    工作示例:http://jsfiddle.net/hunter/XQfgU/(减去 AJAX)

    【讨论】:

    • 根据&lt;a&gt; 标记的href 属性,您可能还想添加function(e)e.preventDefault()
    猜你喜欢
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多