はい/いいえドロップダウンボックスを使用して、CSS を介して条件付きフォーム要素を非表示にしています。フォーム名と要素名を手動で定義するとうまくいきますが、変数を介して定義しようとすると機能しません。私は約1時間トラブルシューティングと調査を行ってきましたが、役に立ちませんでした。これが私のJavascript関数です。
function binaryHideShow(formName, elementName)
{
if (document.forms["'" + formName + "'"].elementName.value == "yes")
{
document.getElementById("'" + elementName + "'").setAttribute("class","show");
}
else
{
document.getElementById("'" + elementName + "'").setAttribute("class","hide");
}
}
そして、これが HTML 要素です。
<select name="PrimaryFiledBankruptcyDropDownList" onchange="binaryHideShow(this.form.name, this.attributes['name'].value);">
<option value=""></option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<div id="PrimaryFiledBankruptcyDropDownList">
<label for="PrimaryBankruptcyTypeDropDownList">Bankruptcy Type:</label>
<select name="PrimaryBankruptcyTypeDropDownList" onchange="">
<option value=""></option>
<option value="chapter-7">Chapter 7</option>
<option value="chapter-13">Chapter 13</option>
</select>
</div>
HTML 側の this ステートメントが正しい要素をプルしていることを既に確認しています。
以下の例のように変数名を手動で入力すると、問題なく動作し、html this ステートメントは以下で使用している正確な名前を返します。
function binaryHideShow()
{
if (document.forms["ProgramApplicationForm"].PrimaryFiledBankruptcyDropDownList.value == "yes")
{
document.getElementById("PrimaryFiledBankruptcyDropDownList").setAttribute("class","show");
}
else
{
document.getElementById("PrimaryFiledBankruptcyDropDownList").setAttribute("class","hide");
}
}