使用以下代码:
JS:
function refreshPrices() {
var currentTotalValue = 0;
$("#results div").each(function() {
if (!isNaN(parseInt($(this).find("span").text().substring(1)))) {
currentTotalValue += parseInt($(this).find("span").text().substring(1));
}
});
$("#totalValue").text("$" + currentTotalValue)
}
要使用它,请将所有 " result*
" 容器存储在一个容器中,这将使输出更容易使用 jQuery 进行迭代[因为只需要一个主要选择器 ( "#results"
) 即可获取所有 " result*
" 容器中的结果]。
<div id="results">
<div id="result">
</div>
<div id="result2">
</div>
</div>
并且每当用户与复选框交互时,refreshPrices()
都应该调用它。
HTML:
<table>
<tr>
<td style="width: 13px">
<input id="1" type="checkbox" name="" onClick="" />
</td>
<td>
<h4>PDF Document</h4>
<p>
Already have your own menu or flyer? Include it in your App
</p>
</td>
<td class="cost" id="pdf-cost">£99.99 per document</td>
</tr>
<tr>
<td style="width: 13px">
<input id="2" type="checkbox"/>
</td>
<td>
<h4>Video</h4>
<p>
If you've already got videos on your website, these can be included within your App too.
</p>
</td>
<td class="cost">£149.99 per video</td>
</tr>
<table>
<br>
<br>
<div id="totalValu">Total Value: <span id="totalValue">$0</span></div>
<div id="results">
<div id="result">
</div>
<div id="result2">
</div>
</div>
JS:
$(document).ready(function() {
$('#1').click(function() {
//var row = $(this);
if (($(this).attr('checked')) == 'checked') {
$('#result').append("-PDF Document <html><span style=float:right>$190</span></html>");
}
else {
$('#result').text("");
}
refreshPrices();
});
$('#2').click(function() {
if (($(this).attr('checked')) == 'checked') {
$('#result2').append("-Video <html><span style=float:right>$150</span></html> ");
}
else {
$('#result2').text("");
}
refreshPrices()
});
});
JSFiddle:http: //jsfiddle.net/BA5rX/7/