开发者

problems with .live and ajax

开发者 https://www.devze.com 2023-04-02 08:49 出处:网络
I have this code: $(document).ready(function() { $(\".vote, .vote1\").click(function() { var id = $(this).attr(\"id\");

I have this code:

$(document).ready(function() 
{
    $(".vote, .vote1").click(function()
    {
        var id = $(this).attr("id");
        var name = $(this).attr("name");
        var dataString = 'id='+ id ;
        var parent = $(this);

        if(name=='mod_up')
        {
            $(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
            $.ajax({
                type: "POST",
                url: "mod_up_vote.php",
                dataType: "xml",
                data: dataString,
                cache: false,
                success: function(xml)
                {
                    //$("#mod-pregunta").html(html);
                    //$("#mod-respuesta").html(html);
                    //parent.html(html);
                    $(xml).find('pregunta').each(function()
                    {
                        var id = $(this).attr('id');
                        var pregunta = $(this).find('preguntadato').text();
                        var respuesta = $(this).find('respuestadato').text();
                        var votoup = $(this).find('votoup').text();
                        var votodown = $(this).find('votodown').text();
                        var id_pregunta = $(this).find('id_pregunta').text();
                        var id_respuesta = $(this).find('id_respuesta').text();
                        $("#mod-pregunta").html(pregunta);
                        $("#mod-respuesta").html(respuesta);
                        $(".vote1").html("<a href=\"\" title=\""+ id_pregunta + "-"+ id_respuesta + "-1\" class=\"vote1\" id开发者_运维知识库=\""+ id_pregunta + "-"+ id_respuesta + "-1\" name=\"mod_up\">Aceptar</a>");
                    });
                }
            });
        }
    });
});

With this link, It update the database and show a new question and answer.

<a href="" title="up" class="vote1" id="<?php echo $id_pregunta."-".$id_respuesta; ?>-1" name="mod_up">Accept</a>

But if I click one more time in Accept's link only shows the gif () And doesn't update the database and doesn't show the new question&answer.

How must I use .live()? or .delegate()? or what i need?


I prefer .delegate. Here's an interesting article about .delegate and .live:

http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/

Here's also what you need to do:

$(document).ready(function() 
{
    $(document).delegate('.vote, .vote1', 'click', function()
    {
        //code
    });
});


Use jQuery live instead of click.

$(".vote, .vote1").live('click', function(){

.......
......


});
0

精彩评论

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

关注公众号