ここでjavascriptを使用した合計noob。関数を変更しようとしています。これは現在そこにあり、機能しているものです。
function hideStateField(theForm) {
theForm.state.disabled = true;
theForm.state.className = 'hiddenField';
theForm.state.setAttribute('className', 'hiddenField');
document.getElementById("stateLabel").className = 'hiddenField';
document.getElementById("stateLabel").setAttribute('className', 'hiddenField');
document.getElementById("stateText").className = 'hiddenField';
document.getElementById("stateText").setAttribute('className', 'hiddenField');
document.getElementById("stateBreak").className = 'hiddenField';
document.getElementById("stateBreak").setAttribute('className', 'hiddenField');
}
「状態」フィールドに固有ではないように、より一般的にしたいと思います。そこで、それを反映するように関数名を変更し、2番目のパラメーターを追加します。次に、「状態」が表示される場所の代わりに、その2番目のパラメーターを変数として使用しようとしています。
function hideAddressField(theForm,theField) {
theForm.theField.disabled = true;
theForm.theField.className = 'hiddenField';
theForm.theField.setAttribute('className', 'hiddenField');
document.getElementById(theField+"Label").className = 'hiddenField';
document.getElementById(theField+"Label").setAttribute('className', 'hiddenField');
document.getElementById(theField+"Text").className = 'hiddenField';
document.getElementById(theField+"Text").setAttribute('className', 'hiddenField');
document.getElementById(theField+"Break").className = 'hiddenField';
document.getElementById(theField+"Break").setAttribute('className', 'hiddenField');
}
2番目の変数として「state」を使用して単純にテストし、機能することを確認しました...実際には機能しませんでした。「UncaughtTypeError:Undefinedのプロパティ'disabled'を設定できません」が表示され続けます。構文エラーだと思います。この関数の呼び出しは次のとおりです。
hideAddressField(theForm,state);
フォームの名前も「theForm」なので、変数「theForm」には「theForm」の値が割り当てられ、変数「theField」には「state」の値が割り当てられており、2つの関数は同等である必要があります。明らかにそうではありません。
どこが間違っているのですか?