[英]Enable/Disable dropdown - Javascript
我有以下代码,其中UI上有2个下拉菜单。 当我选择下拉1的值“1”时,应该禁用下拉2。 当我选择值“1”时,下拉列表2被禁用。 但是当用户从下拉列表1中选择值“2”时,我无法重新启用下拉菜单2.我不确定因为我正在为下拉列表中的两个选项的onClick
事件调用performValidation()
方法。
码:
<html>
<head></head>
<script = "text/javascript">
function performValidation()
{
if (document.mainForm.criteriaOne.value == "1")
{
document.mainForm.criteriaTwo.options.length = 0;
document.mainForm.criteriaTwo.disabled = true;
}
else
document.mainForm.criteriaTwo.disabled = false;
}
</script>
<body>
<form name="mainForm" action= "" method ="get">
Criteria One:<select id = "criteriaOne">
<option value = "1" onClick ="performValidation()"> Select One - One</option>
<option value = "2" onClick ="performValidation()"> Select one - Two</option>
</select>
Criteria Two:<select id = "criteriaTwo">
<option value = "1" onClick ="performValidation()"> Select Two - One</option>
</select>
</form>
</body>
</html>
您需要在选择类似的东西上将函数连接到onchange事件
document.getElementById("criteriaOne").onchange = function()
{ // do some work to check the selectedIndex and determine whether to re- enable the other drop down}
你不一定需要
document.mainForm.criteriaTwo.options.length = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.