jQuery没有选择按钮数据 - 动作值

时间:2015-09-03 10:22:56

标签: javascript jquery twitter-bootstrap

我有这个用于向上/向下拇指的jQuery:

jQuery函数:

$(document).ready(function() {

    //####### on page load, retrive votes for each content
    $.each( $('.voting_wrapper'), function(){

        //retrive unique id from this voting_wrapper element
        var unique_id = $(this).attr("id");

        //prepare post content
        post_data = {'unique_id':unique_id, 'vote':'fetch'};

        //send our data to "vote_process.php" using jQuery $.post()
        $.post('<?PHP echo SITE;?>controller/cm_vote.php', post_data,  function(response) {

                //retrive votes from server, replace each vote count text
                $('#'+unique_id+' .up_votes').text(response.vote_up); 
                $('#'+unique_id+' .down_votes').text(response.vote_down);
            },'json');
    });



    //####### on button click, get user vote and send it to vote_process.php using jQuery $.post().
    $(".voting_wrapper .voting_btn").click(function (e) {

        //get data-action or class name (down_button / up_button) of clicked element
        var clicked_button = $(this).children().attr('data-action');

        //get unique ID from voted parent element
        var unique_id   = $(this).parent().attr("id"); 

        if(clicked_button==='down_button') //user disliked the content
        {
            //prepare post content
            post_data = {'unique_id':unique_id, 'vote':'down'};

            //send our data to "vote_process.php" using jQuery $.post()
            $.post('<?PHP echo SITE;?>controller/cm_vote.php', post_data, function(data) {

                //replace vote down count text with new values
                $('#'+unique_id+' .down_votes').text(data);

                //thank user for the dislike
                alert("Thanks! Each Vote Counts, Even Dislikes!");

            }).fail(function(err) { 

            //alert user about the HTTP server error
            alert(err.statusText); 
            });
        }
        else if(clicked_button==='up_button') //user liked the content
        {
            //prepare post content
            post_data = {'unique_id':unique_id, 'vote':'up'};

            //send our data to "vote_process.php" using jQuery $.post()
            $.post('<?PHP echo SITE;?>controller/cm_vote.php', post_data, function(data) {

                //replace vote up count text with new values
                $('#'+unique_id+' .up_votes').text(data);

                //thank user for liking the content
                alert("Thanks! For Liking This Content.");
            }).fail(function(err) { 

            //alert user about the HTTP server error
            alert(err.statusText); 
            });
        }

    });
    //end 
 });

HTML CODE:

<div class="voting_wrapper" id="<?PHP echo $row['id'];?>">
    <div class="btn-group">
       <div class="voting_btn">
        <button type="button" data-action="up_button" class="btn btn-success btn-xs"><i class="fa fa-thumbs-o-up"></i> <span class="up_votes">0</span></button>
    </div> 
       <div class="voting_btn">
        <button type="button" data-action="down_button" class="btn btn-danger btn-xs "><i class="fa fa-thumbs-o-down"></i> <span class="down_votes">0</span></button>
       </div>
    </div>
</div>

我使用bootstrap 3设计拇指按钮,并为data-actiondown_button添加up_button,并使用此行中的jQuery函数检查data-actionvar clicked_button = $(this).children().attr('data-action');

但是在行动中,我的jQuery功能无法与data-action一起使用,也无法向下发送or向上按钮。

如何解决这个问题?!

1 个答案:

答案 0 :(得分:1)

尝试:

preg_replace("/[\r\n]+/", "\n", $text);

此处提供了文档: https://api.jquery.com/data/