我试图将输入框的值设置为另一个输入框的值乘以一个项目的价格。我把货币包装在一个带类的span标记中。我需要将这个值乘以.productTextInput输入文本框。该值将显示在.subTotal框中。我怎样才能做到这一点?
jQuery:
$(".productTextInput").each(function() {
$(this).change(function() {
var priceAmount = $(this).parents().find(".priceAmount").text();
$(this).parents().find(".subTotal").val(parseInt($(this).val(), 10) * parseInt(priceAmount, 10));
});
});html:
<table class="productTable productSmall" cellspacing="0">
<tbody>
<tr>
<td style="width: 27%;">
<strong>American (wild) plum</strong>
</td>
<td class="custom-tag"></td>
<td class="custom-tag">
Prunus americana
</td>
<td class="custom-tag">
Bare Root
</td>
<td class="custom-tag" style="border-right: 2px solid #000;">
2' - 3' size
</td>
<td style="text-align:right;padding-right: 10px;">
$<span class="priceAmount">1.75</span>
</td>
<td class="quantity">
<input id="Units_4996263" class="productTextInput" name="AddToCart_Amount" value="1" type="text">
</td>
<td>
<input class="subTotal" name="subTotal" readonly="readonly" type="text">
</td>
</tr>
</tbody>
</table>发布于 2015-01-08 20:54:58
在parseFloat上使用priceAmount。parseInt返回一个整数(去掉小数)。parseFloat保留小数。
在当前代码中,priceAmount始终是1,这是由于parseInt。
改为:
$(this).parents().find(".subTotal").val(parseInt($(this).val(), 10) * parseFloat(priceAmount));发布于 2015-01-08 21:10:31
您不需要在这方面使用.each,因为$('.productTextInput')查找提供的类中的每个元素。
我确实在代码段中省略了parseInt/parseFloat,但是JSFiddle有它。http://jsfiddle.net/6qf7oc55/3/
$('.productTextInput').blur(function() {
num = this.value;
price = $(this).closest('tr').find('.priceAmount').text();
sub = num * price;
$('.subTotal').val(sub.toFixed(2));
});
https://stackoverflow.com/questions/27849231
复制相似问题