jQuery如何在ajax回调后重新绑定此事件?

And*_*nda 3 jquery

我正在尝试通过操作jQuery选择器来自己创建一个文本编辑.但是,在第一个ajax回调之后,其他回调相同的输入文本不再起作用了......

function editaVal(celDiv, id)
    {
        var novoConteudo = $("<input type='text' id='novoCont" + id + "' value='" + $(celDiv).html() + "' />");
        $(celDiv).dblclick(function()
        {
            $(this).html(novoConteudo);
        });

        $(novoConteudo).blur(function()
        {
            $(novoConteudo).parents("tr").removeClass('trSelected');
            $.ajax({
                type: 'POST',
                url: '/SuperAdmin/SalvaValor/?busca=' + $(novoConteudo).val() + '&codValor=' + id,
                beforeSend: function()
                {
                    $(celDiv).html($("#loading").show());
                },
                success: function()
                {
                    $(celDiv).html($("#loading").hide());
                    $(celDiv).html(novoConteudo.val());
                }
            });
        });
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是:我怎么能重新绑定这个?重新绑定模糊事件...当我模糊输入文本时,第二个ajax回调没有任何反应.

谢谢!!

CMS*_*CMS 9

你可以使用jQuery.live,(jQuery 1.3+).它将处理程序绑定到所有当前和未来匹配元素的事件 .


Cre*_*esh 5

您将"onblur"处理程序附加到ajax更新后被清除的元素.如果您希望事件持续存在,只需在创建它时绑定(即在上面dblclick):

function editaVal(celDiv, id)
{
    $(celDiv).dblclick(function()
    {
        var text = $(this).html();
        var novoConteudo = $('<input type="text" />')
            .appendTo($(this).empty())
            .attr('id', 'novoCont' + id)
            .val(text)
            .blur(function()
            {
                $(this).parents("tr").removeClass('trSelected');
                $.ajax({
                    type: 'POST',
                    url: '/SuperAdmin/SalvaValor/?busca=' + $(this).val() + '&codValor=' + id,
                    beforeSend: function()
                    {
                        $(celDiv).html($("#loading").show());
                    },
                    success: function()
                    {
                        $(celDiv).html($("#loading").hide());
                        $(celDiv).html(novoConteudo.val());
                    }
                });
            });
    });
}
Run Code Online (Sandbox Code Playgroud)