开发者

merging two small pieces of jquery codes

Following are two pieces of jquery code. First one prints a text message when clicked on a link, and the second slides down a div when click on a link. I want to merge second code into first one, so that when I click the link, it displays the message (as the first code does), and also slides down the #votebox (as done in second code, and show content in that. I will be very thankful for any help.

$("a.vote_up").click(function(){

the_id = $(this).attr('id');
$("span#votes_count"+the_id).fadeOut("fast");
$.ajax({
        type: "POST",
        data: "action=vote_up&id="+$(this).attr("id"),
        url: "votes.php",
        success: function(msg)
        {
            $("span#votes_up"+the_id).fadeOut();
            $("span#votes_up"+the_id).html(msg);
            $("span#votes_up"+the_id).fadeIn();

            //Here, I want to slide down the votebox and content div (from code below).

        }
    });
});

The following code slides down votebox div and displays content in it, I want to include that in the above code.

$("a.vote_up").click(function(){
var id=$(this).attr("id");
var name=$(this).attr("name");
var dataString = 'id='+ id + '&name='+ name;
//I want to include this votebox in above 开发者_运维问答code.
$("#votebox").slideDown("slow");
$("#flash").fadeIn("slow");

$.ajax({
type: "POST",
url: "rating.php",
data: dataString,
cache: false,
success: function(html){
$("#flash").fadeOut("slow");
//and want to use this div as well.
$("#content").html(html);
} 
});
});

Thanks for any help.


Simple way is to define a seperate function for voteDown and call it from the success function: e.g

$("a.vote_up").click(function(){

the_id = $(this).attr('id');
$("span#votes_count"+the_id).fadeOut("fast");
$.ajax({
        type: "POST",
        data: "action=vote_up&id="+$(this).attr("id"),
        url: "votes.php",
        success: function(msg)
        {
            $("span#votes_up"+the_id).fadeOut();
            $("span#votes_up"+the_id).html(msg);
            $("span#votes_up"+the_id).fadeIn();
                        var that = this;
            voteDown.call(that);

        }
    });
});




function voteDown() 
{
    var id=$(this).attr("id");
    var name=$(this).attr("name");
    var dataString = 'id='+ id + '&name='+ name;
    $("#votebox").slideDown("slow");
    $("#flash").fadeIn("slow");

    $.ajax({
        type: "POST",
        url: "rating.php",
        data: dataString,
        cache: false,
        success: function(html){
            $("#flash").fadeOut("slow");
            //and want to use this div as well.
            $("#content").html(html);
        } 
    });
}

Edit: Corrected the JS for Votedown function.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜