[英]Javascript Logic Issue in IE
首先,我将在这里解释一下我的系统。 这种形式可以让孩子们预订复活节和暑假期间在学校开设的一系列日间课程。 该表格允许同时注册两个孩子,并且如果两个孩子的日期相同,则兄弟姐妹可享受5英镑的折扣。
在表单上,有一个日期部分,它是一系列复选框。 当您选中复选框时,将在页面底部计算总价。
单身孩子的价格为29英镑,兄弟姐妹的价格(与第一个孩子匹配的日期)为24英镑。 预订骑马课程需额外支付16.50英镑-但此逻辑错误仅会影响日期选择。
由于某种原因,我无法理解,在Internet Explorer中,注册2个孩子的价格似乎比应有的价格低5英镑。 这是我用于计算日期的代码(下面的功能在日期复选框的onclick上触发):
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条警报之后,我得到了一个奇怪的函数警报,该警报显示字符串长度错误,这是大约四行缩小的代码,因此很难解密。
然后,再次出现相同的警报。
然后我得到“等于”
尝试将循环更改为此:
for (var i=0; i < valsA.length; i++) {
for (var j=0; j < valsB.length; j++) {
if(valsA[i]==valsB[j]){
alert("does equal");
siblingDates=siblingDates+1;
}else{
alert("does not equal");
}
}
}
首先,这些行看起来不对,这是它们在代码中的显示方式吗?
for(var iA=0,elmA;elmA = dates_A[iA];iA++)
for(var iB=0,elmB;elmB = dates_B[iB];iB++)
尝试这样的事情
for(var iA=0;iA < dates_A.length;iA++){
var elmA = dates_A[iA];
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.