[英]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.