开发者

Using one javascript function for multiple inputs on one form

We have a simple age calculating script, that takes a date input (3 selects - day, month, year) and determine how old. It is triggered by an onChange assigned to the year select.

There are several date inputs scattered through the form, each one set up the same, other than the input names (day1 vs day 1 vs day 3, etc).

Currently we have simply duplicated the js code and manually changed the input variables ...

function Age1()

{

var oldDay = document.step1.day1.value;

var oldMonth = document.step1.month1.value;

var oldYear = document.step1.year1.value;

and more script....

function Age2()

{

var oldDay = document.step1.day2.value;

var oldMonth = document.开发者_StackOverflow社区step1.month2.value;

var oldYear = document.step1.year2.value;

and more script....

and so forth.

Ideally we would like to reuse one script, rather than hardcoding one for each instance. I have tried a bunch of ideas to no avail, but the ideal would be to trigger it via: onChange="Age(X);" and then have the js insert the proper variable:

function Age(varX)

{

var oldDay = document.step1.dayX.value;

var oldMonth = document.step1.monthX.value;

var oldYear = document.step1.yearX.value;

and so on ....

Any ideas for us ? Thanks in advance


function Age(varX)
{
    var oldDay = document.step1['day' + varX].value;
    var oldMonth = document.step1['month' + varX].value;
    var oldYear = document.step1['year' + varX].value;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜