使用jQuery将2个HTML类和输出总数相乘

vir*_*s90 0 html javascript jquery multiplication

我想将2以下字段的内容相乘,并在另一个div中输出总数(24*£20.00):

<label class="tm-show-picker-value" for="tmcp_range_4">24</label>
<span class="price amount final">£20.00</span>
Run Code Online (Sandbox Code Playgroud)

我使用jQuery尝试了下面的方法,但我对此并不是很熟悉 - https://jsfiddle.net/e8shpr0e/

$('.total').html(
  parseFloat($('.tm-show-picker-value').text()) * parseFloat($('.price amount final').text())
)
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label class="tm-show-picker-value" for="tmcp_range_4">24</label>
<span class="price amount final">£20.00</span>
<div class="total"></div>
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很棒.

Ji_*_*ing 6

您的代码中存在2个问题.

  1. 你不应该在你的类名中使用空格.
  2. 您的"美元"符号导致解析您的浮动时出错.

使用以下代码.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<label class="tm-show-picker-value" for="tmcp_range_4">24</label>
<span class="price_amount_final">£20.00</span>
<div class="total"></div>



var quantity = parseFloat($('.tm-show-picker-value').text());
var cost = parseFloat($('.price_amount_final').text().replace("£", ""));
$('.total').text("£" + quantity * cost );
Run Code Online (Sandbox Code Playgroud)

  • 或者OP可以使用这个选择器:`.price.amount.final` (4认同)