我需要访问页面中多个字段的值并对它们执行相同的计算。我想做这样的事情,允许我动态访问不同的表单元素,但我无法让它工作:
function numInFamily(famID) {
var numAdults;
var numChildren;
var adultFieldID;
var childFieldID;
adultFieldID = 'numAdultsFam' + toString(famID);
childFieldID = 'numKidsFam' + toString(famID);
numAdults = parseInt(document.getElementById(adultFieldID).value,10);
numChildren = parseInt(document.getElementById(childFieldID).value,10);
return numAdults + numChildren;
}
谁能解释我如何动态引用这些元素?