为什么这个简单的函数不起作用
Why is this simple function not working
为什么不工作?类似的代码在这里工作:http://www.ralphphillips.com/youtube/stick-figure/stick-figure2.html
但这不起作用。我有正确的html代码。设置了ID,但每个ID不同。它甚至没有给出输出0来表示total是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>
我将重写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。
我可能在这里说的很明显,但是第一个函数(updateTotal)缺少右括号。如果您从源代码复制和粘贴,那么这可能是您的问题。当你盯着它看的时候很容易忽略;-)
我会试试这个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>
相关文章:
- Ember Data DS.Model's set函数不起作用
- 为什么我的JavaScript堆栈排序函数不起作用
- Javascript onchange()函数不起作用
- 另一个Ajax函数触发的Ajax函数不起作用
- 从PHP调用JS函数不起作用
- jQuery validate函数不起作用
- JS-窗口宽度函数不起作用
- javascript函数,该函数不起作用,但不会显示任何错误
- 为什么这个排序函数不起作用
- 为什么这个反向函数不起作用
- Javascript parseInt 函数不起作用
- 我的函数不起作用,Ajax调用,JQuery,调用外部?我不知道
- 主干构造函数不起作用
- Javascript:this.value 函数不起作用
- Mocha 的 beforeEach() 和 done() 函数不起作用
- 在jQuery中,我的函数不起作用
- jquery-ui-rails的draggable和dropable函数不起作用
- 数学函数不起作用
- Javascript-创建打印到HTML的函数不起作用
- 我上传了客户's服务器一个js函数不起作用