我使用yes/no dropdownbox來通過CSS隱藏條件表單元素。它工作正常,如果我手動定義表單名稱和元素名稱,但如果我嘗試通過變量定義它們將無法正常工作。我一直在進行故障排除和研究大約一個小時,但無濟於事。這是我的Javascript功能。使用Javascript函數變量定義DOM元素
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側這一聲明是拉動正確的元素。
要清楚,如果我手動輸入變量名稱按照下面的例子,它將正常工作,並且這個語句的html返回下面我使用的EXACT名稱。
function binaryHideShow()
{
if (document.forms["ProgramApplicationForm"].PrimaryFiledBankruptcyDropDownList.value == "yes")
{
document.getElementById("PrimaryFiledBankruptcyDropDownList").setAttribute("class","show");
}
else
{
document.getElementById("PrimaryFiledBankruptcyDropDownList").setAttribute("class","hide");
}
}
這似乎很奇怪,我說你隱藏在其中選擇是相同的下拉列表/不,你不應該隱藏**其他**下拉列表嗎? – 2012-07-12 21:13:28