开发者

jQuery find all elements with specific class

开发者 https://www.devze.com 2023-02-10 00:30 出处:网络
I\'m using jQuery AJAX to implement a voting system. I\'m using this code: $(\".vote_up\").click(function(){

I'm using jQuery AJAX to implement a voting system. I'm using this code:

$(".vote_up").click(function(){
    var target = $(this);
    var id = this.id;
    var vote = $(this).attr("class");
    var voteCount = $(this).next('p').attr("class");
    var data = "id=" + id + "&vote=" + vote;
    $.ajax
        ({
            context: this,
            type: "POST",
            url: "vote.p开发者_开发百科hp",
            data: data,
            dataType: "json",
            cache: false,
            success: function(data)
            {
                target.attr('disabled','disabled');
                for(var x in data) {
                        $("." + voteCount).html(data[x].vote_up);
                }
            }
    });
});

I have is an input and a corresponding <p> element via this code:

while ($row = mysql_fetch_array($result)){
    echo "<p>" . $row['content'] . "</p>";
    echo "<p>Posted by " . $row['username'] . "</p>";
    if($_SESSION['username']){ 
        echo "<input type='button' id='".$row['id']."' class='vote_up' value='vote up'></input>";
    }
    echo "<p class='votes_up ".$row['id']."'>" . $row['vote_up'] . "</p>";
    if($_SESSION['username']){ 
        echo "<input type='button' id='".$row['id']."' class='vote_down' value='vote down'></input>";
    }
    echo "<p class='votes_down ".$row['id']."'>" . $row['vote_down'] . "</p>";
    echo "<hr />";
}

When the user presses on a vote_up or vote_down button for the jQuery success function, it should update all <p> elements on the page with their class name as the same as the input button's ID. For example, if input was pressed with ID 12, then there will be 5 <p> elements with class name of 12.


ID starting with numbers are invalid.

$(".vote_up").click(function(){
    var clickedElemid = this.id;

    var pElems = $("p." + clickedElemid);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消