在DropDownList中onChange和onSelect [英] onChange and onSelect in DropDownList

查看:119
本文介绍了在DropDownList中onChange和onSelect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DropDownList,询问用户他是否想加入俱乐部:

 您是否想加入俱乐部

不是

在此列表中还有另一个列表,它被设置为作为默认值禁用。这个名单有俱乐部的部门。这个列表在用户选择Yes之前不会被启用。



我构建了下面的代码,但是我无法解决的问题是让我们假设用户选择Yes,然后他改变他的决定,所以他会再次选择No。在这种情况下,部门列表仍然可用。我希望在他再次选择No时禁用它。

 < html> 
< head>
< script type =text / javascript>
function disable()
{
document.getElementById(mySelect1)。disabled = true;
}
function enable()
{
document.getElementById(mySelect1)。disabled = false;
}
< / script>
< / head>
< body>
< form>
< select id =mySelectonChange =enable();>
< option onSelect =disable();>否< / option>
< option onSelect =enable();>是< / option>
< / select>
< select id =mySelect1disabled =disabled>
< option> Dep1< / option>
< option> Dep2< / option>
< option> Dep3< / option>
< option> Dep4< / option>
< / select>
< / form>
< / body>
< / html>

我认为 onSelect =disable();

谢谢

解决方案

我会这样做 jsFiddle示例

JavaScript:

  function check(elem){
document.getElementById('mySelect1')。disabled =!elem.selectedIndex;
}

HTML:

 <形式> 
< select id =mySelectonChange =check(this);>
< option>无< / option>
< option>是< / option>
< / select>

发送“验证码”获取 | 15天全站免登陆