简体   繁体   中英

How to show display and hide text instead of using alert in javascript?

Here is javascript for up and down vote click function:

<script type="text/javascript">
$(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('vote_process.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 class name (down_button / up_button) of clicked element
        var clicked_button = $(this).children().attr('class');

        //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('vote_process.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('vote_process.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 

});
</script>

html:

<div class="content_wrapper">
    <h3><img src="9780143332497.jpg" alt="">
        <!-- voting markup -->
        <div class="voting_wrapper" id="1001">
            <div class="voting_btn">
                <div class="up_button">&nbsp;</div><span class="up_votes">0</span>
            </div>
                    </div>
        <!-- voting markup end -->
    </h3>

    </div>

When click the button, it shows the text as alert box,

May i know, how to shows text and hide like animate,

I need when i click the button, it shows "you voted" and after that it hide.

Anyone can help me? Thanks in advance.

You need to create a div with an id and put it where you want to show the text. Then you can replace your alert statement with something like

$('#myDiv').text('My Text!');

or

$('#myDiv').innerHtml('<span>My Text!</span>');

When the Button is clicked, create a div container and show it. Then wait 2000ms and then fade it out!

$("#myButton").click(function() {
        var myAlert = $('<div />').addClass("alert alert-success").html("test");
        $("body").append(myAlert);
        setTimeout(function() {
            $(myAlert).fadeOut();
        },2000);
    });

What you want to use is called modal/popup/dialog. Here you have an example of using modal in bootstrap. All you need to do it to replace alert() in your code with properly used modal window.

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