【发布时间】:2012-07-12 18:48:04
【问题描述】:
我正在使用是/否下拉框通过 CSS 隐藏条件表单元素。如果我手动定义表单名称和元素名称,它工作正常,但如果我尝试通过变量定义它们,它将不起作用。我已经进行了大约一个小时的故障排除和研究,但无济于事。这是我的 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");
}
}
【问题讨论】:
-
您隐藏了选择是/否的同一个下拉列表对我来说似乎很奇怪,您不应该隐藏 other 下拉列表吗?
标签: javascript forms function variables