1 に答える
1
まず、あなたは年齢を奇妙に計算しています。
これを行う:
var today = new Date();
var bday = new Date(d[2],d[1],d[0]);
var age = today.getFullYear() - bday.getFullYear(); //If you're born in 1980 then 2011-1980 means you're about 31 years old, this will give you the age if birthday has already passed
if(today.getMonth() < bday.getMonth() || (today.getMonth() == bday.getMonth() && today.getDate() < bday.getDate()))
{
age--; //Reduce age by 1 if birthday hasn't passed
}
巨大な if elseif 構造については、まず 2 つの別個の構造に分割します。これにより、一方では利用できない範囲を利用して、全体的な構造を短くすることができます。年齢層がごちゃごちゃしているので、適切な構造を構築するためにそれを拾う気がしません. if/elseif/else の代わりに switch case ステートメントを利用したい場合があります。
また、関数が属する head に関数を配置します。また、有効な日付でない場合に備えて、ShowAge() 関数のエラー チェックが必要です。getElementById
また、最後にフォームの値を変更するには、フォームの id を使用する必要があります。ShowAge()
そうしないと、何が何でform
あるかわかりません。したがって、フォームに id を指定し、末尾を次のように変更ShowAge()
します。
document.getElementById('formid').age.value = age;
于 2011-12-22T00:39:11.917 に答える