javascript 禁用/启用复选框和下拉菜单
您好,我正在尝试在选中复选框时禁用下拉菜单,而在取消选中复选框时启用下拉菜单。下面是我的 javascript,当我选中复选框时它正在工作,但它不会让我取消选中复选框:
<script type="text/javascript">
function test(obj){
if (document.getElementsByName("aForm[0].info")[0].checked = true) {
alert("here");
document.getElementsByName("aForm[0].gender")[0].disabled = true;
}
else document.getElementsByName("aForm[0].info")[0].checked = false;{
document.getElementsByName("aForm[0].gender")[0].enabled = true;
}
}
</script>
这是我的 jsp
<logic:iterate id="data" name="aForm" property="testList">
<tr>
<td>
<html:checkbox indexed="true" name="aForm" styleId="something" property="info" onclick="test(this)"/>
</td>
<td>
<html:select name="aForm" indexed="true" property="unit" styleId="dropdown">
<html:optionsCollection name="aForm" property="selectGender" value="value" label="label"/>
</html:select>
>
</td>
</tr>
</logic:iterate>
Hi I am trying to disable a drop down when a checkbox is checked and the when the checkbox is unchecked enable the drop down. below is my javascript and it is working when I check the checkbox but it won't let me uncheck the checkbox:
<script type="text/javascript">
function test(obj){
if (document.getElementsByName("aForm[0].info")[0].checked = true) {
alert("here");
document.getElementsByName("aForm[0].gender")[0].disabled = true;
}
else document.getElementsByName("aForm[0].info")[0].checked = false;{
document.getElementsByName("aForm[0].gender")[0].enabled = true;
}
}
</script>
and here is my jsp
<logic:iterate id="data" name="aForm" property="testList">
<tr>
<td>
<html:checkbox indexed="true" name="aForm" styleId="something" property="info" onclick="test(this)"/>
</td>
<td>
<html:select name="aForm" indexed="true" property="unit" styleId="dropdown">
<html:optionsCollection name="aForm" property="selectGender" value="value" label="label"/>
</html:select>
>
</td>
</tr>
</logic:iterate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在存储一个值,而不是比较
if 语句
应该
注意 ==
而 else 是什么?
您将其视为 else if(),else 那里没有语句。
您的代码在运行时应该抛出 JavaScript 错误。
并且没有启用这样的东西,它只是禁用,true/false。
您的代码应该如下所示
,并且可以简化为仅 3 行:
并使其在没有元素名称的情况下工作:
注意:如果页面结构发生变化,parentNode.parentNode 必然会中断。 :)
You are storing a value, not comparing
The if statement
should be
notice the ==
And what is with the else?
You are treating it like an else if(), else does not have a statement there.
Your code should throw JavaScript errors when run.
And there is no such thing as enabled, it is just disabled, true/false.
Your code should look like
and it can be simplified down to just 3 lines:
and to make it work without the element names:
Note: parentNode.parentNode is bound to break if the page structure changes. :)