【问题标题】:problems with .live and ajax.live 和 ajax 的问题
【发布时间】:2011-09-05 21:40:17
【问题描述】:

我有这个代码:

$(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>");
                    });
                }
            });
        }
    });
});

通过此链接,它会更新数据库并显示新的问题和答案。

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

但是如果我再点击一次Accept的链接只显示gif()并且不更新数据库也不显示新的问题和答案。

我必须如何使用 .live()?还是.delegate()?或者我需要什么?

【问题讨论】:

    标签: php jquery mysql ajax


    【解决方案1】:

    我更喜欢 .delegate。这是一篇关于 .delegate 和 .live 的有趣文章:

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

    这也是你需要做的:

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

    【讨论】:

    • 我正在写一个有很多 o 的大英雄联盟。但是有人会标记它,我可怜的 19 枚硬币会被扔进垃圾桶。真的谢谢。 (爱你)
    【解决方案2】:

    使用jQuery live 而不是click。

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

    【讨论】:

    • 我做了,也绑定了。$(document).ready(function() { $(".vote, .vote1").live(function(){ { var id = $(this ).attr("id"); var name = $(this).attr("name"); .... 和 ... 这不起作用
    • 如果我第一次点击它,我会被重定向到家。不知道为什么。
    • 如果我将
    • 我投了赞成票,因为你在这里帮助了太多@Shankar 和其他帖子。但有一件事,在官方文档中我看到了这个: $('.clickme').live('click', function() {
    • 谢谢@Jorge 我实际上错过了click 的事件类型,它是live 方法的第一个参数。那是匆忙:)
    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    相关资源
    最近更新 更多