简体   繁体   中英

jQuery find all elements with specific class

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.php",
            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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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