doulanli6146 2011-09-05 21:40
浏览 4
已采纳

.live和ajax的问题

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?

  • 写回答

2条回答 默认 最新

  • dougaxing8673 2011-09-05 21:50
    关注

    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
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集