我不明白为什么 .parent()
不能处理动态创建的元素。
这里是 fiddle显示我的问题。它计算静态元素的折扣价,而不是创建的元素。
$('.item-sizes .discount-percent').live('keydown', function () {
var percentValue = $(this).val();
var basePrice = $('.base-price', $(this).parent().parent()).val(); // Undefine on live elements, defined on static
if (basePrice) {
discountValue = basePrice - ((percentValue / 100) * basePrice);
$('.discount-price', $(this).parent().parent()).val(discountValue);
}
});
最佳答案
您要添加 的 HTML 在任何地方都没有 base-price
类。
关于javascript - parent() 不适用于事件元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12716530/