javascript - 使用javascript根据其他下拉列表中的选定值启用或禁用下拉列表

标签 javascript html html-select disabled-input

当我从下拉列表中选择 Student 选项时,我想从元素中删除一个属性。如果我选择 TeacherStaff,类(class)和年级将被禁用,但如果我选择 Student,它将启用 thanks等待您的回复

HTML:

<div class="control-group">
    <label class="control-label" for="inputPassword">Type:</label>
    <div class="controls">
        <select name="type" id="type" required>
            <option></option>
            <option>Student</option>
            <option>Teacher</option>
            <option>Staff</option>
            <option></option>
        </select>
    </div>
</div>

<div class="control-group">
    <label class="control-label" for="inputPassword">Course Type:</label>
    <div class="controls">
        <select name="course" id="course" required>
            <option></option>
            <option>BSIT</option>
            <option>BSCS</option>
            <option>BSHRM</option>
            <option>BSBM</option>
            <option>BSTM</option>
            <option>COE</option>
            <option></option>                                   
        </select>
    </div>
</div>

<div class="control-group">
    <label class="control-label" for="inputPassword">Year Level:</label>
    <div class="controls">
        <select name="year_level" id="year_level">
            <option> </option>
            <option>First Year</option>
            <option>Second Year</option>
            <option>Third Year</option>
            <option>Fourth Year</option>    
        </select>
    </div>
</div>

这是 JavaScript:

<script>    
    document.getElementById('type').onchange = function () {  
        var obj = document.getElementById('course').setAttribute('disabled',this.value=='Student');

        document.getElementById('course').setAttribute('disabled',this.value=='Teacher');
        document.getElementById('year_level').setAttribute('disabled',this.value=='Teacher');

        obj.setAttribute('disabled');
        obj.removeAttribute('disabled');
    }    
</script>

下拉: drop-down print-screen .

示例: https://jsfiddle.net/dzpr46d4/

最佳答案

要再次启用下拉菜单,您需要使用以下代码删除 disabled 属性:

document.getElementById('course').removeAttribute('disabled');

我有edited your JSFIDDLE DEMO .这是代码:

     document.getElementById('type').onchange = function () {
     alert("selected value = "+this.value);
     if(this.value == "Student")
     {
            document.getElementById('course').removeAttribute('disabled');
document.getElementById('year_level').removeAttribute('disabled');

     }
     else
     {
            document.getElementById('course').setAttribute('disabled', true);
                    document.getElementById('year_level').setAttribute('disabled', true);

     }
      }  

关于javascript - 使用javascript根据其他下拉列表中的选定值启用或禁用下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45294280/

相关文章:

javascript - Grails 如何在每个表单中获取我的表单/文本框值

javascript - jquery改变背景大小

struts2 - 如何在 Struts2 <s :select> tag? 中设置默认选定值

javascript - 基于日历中的用户输入,突出显示时间和星期几

html - 当您尝试选择选项时,CSS 下拉菜单消失

javascript - 为什么YouTube Player API循环无效?

javascript - 为什么在为名称为 "datetype"的 radio 输入类型执行 html 发布时,它没有正确发布

javascript - 通过 jQuery 访问 HTML 表单

javascript - 如何在我的网站上将 Internet 上的 JSON 文件中的数据显示为 HTML?

javascript - 如何强制 TinyMCE 在 P 标签内创建 SPAN 标签?