jQuery 模糊事件只触发一次
jQuery blur event fires only once
我正在制作一个脚本来管理购物车,我想将模糊事件附加到输入字段,您可以在其中更改产品的数量。
问题是事件只触发一次。之后我必须刷新页面才能再次启用它。
现在我有这个:
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);
}
});
}
第一次效果很好,但后来活动就失效了。关于我做的有多糟糕有什么建议吗?
将事件绑定到 dom 上的元素(无论它们是新元素还是预先存在的元素)的失败证明语法是
$(document).on('focusout', '.fila_producto', function() {
// do stuff here
});
$(document)
也可以是更具体的选择器,其中包含 .fila_producto
class(但请确保您使用的任何选择器预先存在于 dom, 不是添加的元素)。希望这对您有所帮助!
这应该可以满足您的要求。
(function($){
$(document).on('blur',
'input,.fila_producto',
{param: $(this).parent().parent().attr('id')},
updateCantidadProducto
);
}(jQuery));
我正在制作一个脚本来管理购物车,我想将模糊事件附加到输入字段,您可以在其中更改产品的数量。
问题是事件只触发一次。之后我必须刷新页面才能再次启用它。
现在我有这个:
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);
}
});
}
第一次效果很好,但后来活动就失效了。关于我做的有多糟糕有什么建议吗?
将事件绑定到 dom 上的元素(无论它们是新元素还是预先存在的元素)的失败证明语法是
$(document).on('focusout', '.fila_producto', function() {
// do stuff here
});
$(document)
也可以是更具体的选择器,其中包含 .fila_producto
class(但请确保您使用的任何选择器预先存在于 dom, 不是添加的元素)。希望这对您有所帮助!
这应该可以满足您的要求。
(function($){
$(document).on('blur',
'input,.fila_producto',
{param: $(this).parent().parent().attr('id')},
updateCantidadProducto
);
}(jQuery));