为什么这不起作用?类似的代码在这里工作:http://www.ralphphillips.com/youtube/stick-figure/stick-figure2.html
但这不起作用。我有正确的HTML代码。 ID已设置但每个ID都不同。它甚至不输出0表示总数为0.没有值出现。
<!-----SAMPLE INPUT ---->
<input type="radio" autocomplete="off" id="pack11049" name="radio-73" value="1"
onkeyup="updateTotal()">
<script language="javascript">
function updateTotal() {
var optionsPrice = 0;
var accompPrice = 0;
function checkOptions() {
if (document.getElementById('pack11049').checked) {
optionsPrice += 1049;
}
if (document.getElementById('pack21199').checked) {
optionsPrice += 1199;
}
if (document.getElementById('pack31199').checked) {
optionsPrice += 1199;
}
if (document.getElementById('pack41299').checked) {
optionsPrice += 1299;
}
if (document.getElementById('pack61499').checked) {
optionsPrice += 1499;
}
if (document.getElementById('pack71549').checked) {
optionsPrice += 1549;
}
if (document.getElementById('pack81699').checked) {
optionsPrice += 1699;
}
if (document.getElementById('pack91799').checked) {
optionsPrice += 1799;
}
if (document.getElementById('pack101999').checked) {
optionsPrice += 1999;
}
if (document.getElementById('pack112499').checked) {
optionsPrice += 2499;
}
if (document.getElementById('pack122549').checked) {
optionsPrice += 2549;
}
} // end of checking for Package
function checkAccomp() {
if (document.getElementById('howmany').value == '1') {
accompPrice += 129;
}
if (document.getElementById('howmany').value == '2') {
accompPrice += 258;
}
if (document.getElementById('howmany').value == '3') {
accompPrice += 1057;
}
if (document.getElementById('howmany').value == '4') {
accompPrice += 1856;
}
} // end of check accomp function
checkPackage();
checkAccomp();
var totalPrice = optionsPrice + accompPrice;
document.getElementById('optionsPrice').innerHTML = "$ " + optionsPrice;
document.getElementById('accompPrice').innerHTML = "$ " + accompPrice;
document.getElementById('totalPrice').innerHTML = "$ " + totalPrice;
} // end of my main update total function
</script>
答案 0 :(得分:3)
我会重写checkOptions
和checkAccomp
函数并使用以下模式删除所有这些if
语句:创建一个存储每个复选框的id及其相应价格的对象,然后使用带有键值查找的for-in
循环,如下所示:
var OptionPricing = {
'pack11049': 1049,
'pack21199': 1199,
'pack31199': 1199,
'pack41299': 1299,
'pack61499': 1499
};
var AccompPricing = {
0: 0,
1: 129,
2: 258,
3: 1057,
4: 1856
};
function checkOptions() {
var Price = 0;
for (Packs in OptionPricing) {
if ($('#' + Packs).is(':checked')) {
Price += OptionPricing[Packs];
}
}
return Price;
}
function checkAccomp() {
var Accomp = parseInt($('#HowMany').val(), 10);
return AccompPricing[Accomp];
}
function updateTotal() {
var ThePrice = checkOptions() + checkAccomp();
$('#TotalPrice').text(ThePrice);
}
$(function () { $('.DoPricing').click(updateTotal); });
这是工作jsFiddle。我没有将所有ID和相应的价格添加到OptionPricing对象中,但您明白了。此外,如果价格发生变化,或者如果添加新价格,这种模式应该更容易维护,更不用说代码大大减少到几行(如果你喜欢简洁的语法,甚至可以进一步减少) )。我使用了jQuery(你在问题中有标签)但你可以很容易地修改它并在需要时使用普通的js。
答案 1 :(得分:1)
我会尝试这个http://jsfiddle.net/EFWf5/1:
$("form#order :input[type=radio]").each(function(){
$(this).bind('change', function() {
checkOptions();
});
});
var checkOptions = function () {
var total = 0;
var pack = parseInt($('input[name=pack]:checked', '#order').val());
var accomp = parseInt($('input[name=accomp]:checked', '#order').val());
if (!isNaN(pack)) total += pack;
if (!isNaN(accomp)) total += accomp;
$('#total').text(total);
$('#title').text($('input[name=pack]:checked', '#order').attr('title'));
};
<form id="order">
<div class="col">
<strong>Package</strong><br /><hr />
<input type="radio" name="pack" value="1049" />Pack 1<br />
<input type="radio" name="pack" value="1199" title="MVP Package"/>Pack 2<br />
<input type="radio" name="pack" value="1199" title="Oceanview" />Pack 3<br />
<input type="radio" name="pack" value="1299" />Pack 4<br />
<input type="radio" name="pack" value="1499" />Pack 5</div>
<div class="col">
<strong>Accomp</strong><br /><hr />
<input type="radio" name="accomp" value="129" />1<br />
<input type="radio" name="accomp" value="258" />2<br />
<input type="radio" name="accomp" value="1057" />3<br />
<input type="radio" name="accomp" value="1856" />4
</div>
</form>
<div class="clear"></div>
<hr id="bar" />
<div class="clear"></div>
<div>
<div class="col">
<strong>Total: </strong><span id="total">0</span>
</div>
<div class="col" style="width:200px">
<strong>Title: </strong><span id="title"></span>
</div>
</div>
答案 2 :(得分:1)
我可能会在这里说明显而易见的,但第一个函数(updateTotal)缺少结束括号。如果您从源中复制并粘贴,则可能是您的问题。当你盯着它时容易被忽视; - )