【发布时间】:2019-03-03 01:04:03
【问题描述】:
目前,我可以输出它们的每个值并将其显示为一个系列,但我想要的是对它们的所有值求和并显示其总数。
这是我的示例代码:
Javascript
$(function() {
$('input[name=selectProducts]').on('change', function() {
$('#products').text($('input[name=selectProducts]:checked, input[name=selectProducts][type=text]'').map(function() {
return this.value;
}).get());
});
});
HTML
<input type="checkbox" name="selectProducts" id="product1" value="5" />
<input type="checkbox" name="selectProducts" id="product2" value="5" />
<input type="checkbox" name="selectProducts" id="product3" value="5" />
<!-- i want to include these input type text -->
<input type="text" name="selectProducts" id="product4" value="10" />
<input type="text" name="selectProducts" id="product5" value="10" />
<span name="products" id="products"></span>
输出
5,5,5,10,10
35 <!-- my desired output should look like this -->
我想将所有这些相加得到35 的总数。
请帮帮我。
【问题讨论】:
标签: javascript jquery html html-input