【发布时间】:2011-03-09 10:40:57
【问题描述】:
首先,我将在这里稍微解释一下我的系统。这是一种允许孩子预订在学校复活节和暑假期间开设的一系列日间课程的表格。该表格允许同时注册两个孩子,如果两个孩子的日期相同,兄弟姐妹的日期折扣为 5 英镑。
在表单上,有一个日期部分,它是一系列复选框。当您选中复选框时,将在页面底部计算总价。
独生子女价格为 29 英镑,兄弟姐妹价格(与第一个孩子相匹配的日期)为 24 英镑。预订骑马课程需额外支付 16.50 英镑 - 但此逻辑错误仅适用于日期选择。
出于某种我无法理解的原因,在 Internet Explorer 中注册 2 个孩子的价格似乎比应有的低 5 英镑。这是我计算日期的代码(下面的函数在点击日期复选框时触发):
function processDates(){
//contentsA and contentsB are used for outputting on the email confirmation
valsA = [], dates_A = document.forms['registerForm']['childADates[]'];
for(var iA=0,elmA;elmA = dates_A[iA];iA++) {
if(elmA.checked) {
valsA.push(elmA.value);
}
}
contentsA = valsA.join(', ');
//this generates a string based on what dates were selected i.e. April 18th, April 19th etc.
var valsB = [], dates_B = document.forms['registerForm']['childBDates[]'];
for(var iB=0,elmB;elmB = dates_B[iB];iB++) {
if(elmB.checked) {
valsB.push(elmB.value);
}
}
contentsB = valsB.join(', ');
//same as contentA but for the second child.
//get the total dates for both children
//fullDates is the number of dates selected (number of checkboxes checked)
fullDates = (valsA.length + valsB.length);
siblingDates=0;
//this detects if entries are matching in the two arrays valsA and valsB
for(var i in valsA) {
for(var j in valsB) {
if(valsA[i]==valsB[j]){
//if there are matching dates, increment a siblingDates value to get a total number of matching dates in the siblingDates variable
siblingDates=siblingDates+1;
}
}
}
//get the amount of dates to be charged at £29
fullDates = fullDates - siblingDates;
fullPrice = fullDates*29;
siblingPrice = siblingDates*24;
totalPrice = fullPrice + siblingPrice;
calcTotal();
}
function calcTotal(){
var horseA = parseInt(document.getElementById("horseridingA").value);
var horseB = parseInt(document.getElementById("horseridingB").value);
var horse = parseInt(horseA+horseB);
//Add the horseriding price
overall = parseFloat(horse*16.5)+totalPrice;
//output the overall total to the form
document.getElementById("totalPrice").innerHTML = overall;
}
更新:当用户选择对应于每个日期的复选框之一时,将运行此过程。 4 月 18 日 []、4 月 19 日 [] 等。这些复选框为第二个孩子重复。单击其中一个复选框后,将运行上述函数,计算屏幕底部的总价。在 Internet Explorer 中,单击 ChildA 的日期会产生 24 英镑而不是 29 英镑,但仅在单击的第一个日期,childA 和 childB 的所有其他日期都会正确计算。第一个约会选择比应有的少 5 英镑。
我刚刚使用 IE 选择了主要子项的第一个日期,它给出了 24 英镑,然后取消选中了给出 -5 英镑的同一个复选框
更新 2: 好的!我已将问题缩小到以下陈述:
fullDates = (valsA.length + valsB.length);
siblingDates=0;
for(var i in valsA) {
alert(valsA[i]);
for(var j in valsB) {
if(valsA[i]==valsB[j]){
alert("does equal");
siblingDates=siblingDates+1;
}else{
alert("does not equal");
}
}
}
点击 4 月 18 日复选框后,第一个警报 (valsA[i]) 显示为 4 月 18 日
第二个警报按预期显示为“不等于”。
在 Internet Explorer 中,上述情况正常发生,但我收到了一组额外的警报:
在上述 2 个警报之后,我收到一个奇怪的函数警报,提示字符串长度错误,它大约是四行压缩代码,非常难以破译。
然后同样的警报再次出现。
然后我得到“等于”
【问题讨论】:
-
我认为通过您在表单上选择的示例可以更清楚地说明这一点。当您看到错误时,表单的确切情况是什么?
-
当用户选择对应于每个日期的复选框之一时运行此过程。 4 月 18 日 []、4 月 19 日 [] 等。这些复选框为第二个孩子重复。单击其中一个复选框后,将运行上述函数,计算屏幕底部的总价。在 Internet Explorer 中,单击 ChildA 的日期会产生 24 英镑而不是 29 英镑,但仅在单击的第一个日期,childA 和 childB 的所有其他日期都会正确计算。第一个日期选择比应有的少 5 英镑
-
contentA和contents B的值是多少?
-
它们只是用于数据库条目并以不同的功能发送邮件,它会给出一个组合日期的字符串,即 4 月 18 日、4 月 19 日、4 月 20 日等。它们不是计算错误。
-
我只是想看看里面到底有什么。远程调试很难! :) 你说它们不在计算中,但如果你在减去兄弟日期之前显示了这些值加上 fullDates 和兄弟日期的值,那么就会更容易理解哪里出了问题。
标签: javascript internet-explorer function logic