我剛剛就我已經找到的答案發表了評論;不過,我最近創建了一個帳戶,並沒有所需的聲譽。答案是這樣的鏈接:使用循環收集變量
Calculate average of 5 numbers using 2 functions and onchange event handlers
如果向下滾動,它是由RobG給出了答案。在他的腳本中,他提到你可以在循環中收集變量而不是手動調用變量,但他沒有說明如何做到這一點。如果有人可以給我一些關於如何做到這一點的見解,將不勝感激。
在此先感謝,因爲我花了近7個小時試圖弄清楚這一點。
這裏是從另一個頁面的代碼:
function calcAvg(one, two, three, four, five) {
// Note that these could be collected using a loop
var one = document.totalf.one.value;
var two = document.totalf.two.value;
var three = document.totalf.three.value;
var four = document.totalf.four.value;
var five = document.totalf.five.value;
// At this point you'd normally validate the values retrieved from
// the form and deal with any junk (remove it, stop processing,
// ask for another value, etc.)
// pass values to performCalc and store result
var average = performCalc([one, two, three, four, five]);
// Now do something with the result
document.totalf.res.value = average;
// There's no need for a return statement as
// the function doesn't need to return a value
// Though an empty return statement is harmless
// return
}
這是一個註釋現有的答案。 –
@limelights這個問題本來可以做得更好(其他代碼可能已經被複制過,但是其他的答案並不包括這裏尋找的答案)。 – Pointy
我想說這個問題可能是有效的,如果你改寫它。因爲它很快就會被關閉。 – Stephan