jQuery模糊事件只触发一次

时间:2015-01-29 21:25:50

标签: jquery ajax onblur

我正在编写一个脚本来管理购物车,我想在输入字段中附加一个模糊事件,您可以在其中更改产品的数量。

问题是事件只发生一次。之后我必须刷新页面才能再次启用它。

现在我有这个:

    jQuery(document).ready(function($) {
    $("input",".fila_producto").each(function() {
        $(this).on('focusout', {param: $(this).parent().parent().attr('id')}, updateCantidadProducto);
    });
});

在脚本中,我使用行的ID作为参数,我正在使用输入来检查其值。

发出请求的功能就是这个:

function updateCantidadProducto(event){
    var id = event.data.param;
    var cantidad = $(this).val();

    var datos = {   
        "modify" : true,
        "identificador" : id,
        "cantidad": cantidad
    };

    $.ajax({
        data: datos,
        url: 'carrito.php',
        type: 'post',
        success: function(response){
            $("#aside").replaceWith(response);
        }
    }); 

}

第一次效果很好,但事件不起作用。我有什么不好的建议?

2 个答案:

答案 0 :(得分:1)

将事件绑定到dom上的元素的失败证明语法(无论它们是新元素还是预先存在的元素)是

$(document).on('focusout', '.fila_producto', function() {
   // do stuff here
});

$(document)也可以是一个更具体的选择器,其中包含.fila_producto类(但要确保您使用的任何选择器都预先存在于dom中,而不是添加的元素)。希望这有帮助!

答案 1 :(得分:1)

这应该做你要求的。

(function($){
    $(document).on('blur',
                  'input,.fila_producto',
                  {param: $(this).parent().parent().attr('id')},
                   updateCantidadProducto
                  );
}(jQuery));