我不明白为什么,但.parent()
没有处理动态创建的元素。
这是显示我的问题的小提琴。它计算静态元素的折扣价格,而不是创建的元素的折扣价格。
$('.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
类。
您在错误的元素上具有base-price
类:
<input type="text" name="text[]" placeholder="foo" class="span3 base-price"/>
这解释了NaN
响应
这里是固定的:
http://jsfiddle.net/znvHa/5/