简体   繁体   English

使用JavaScript禁用下拉菜单

[英]Disable the drop down using JavaScript

There are two drop down boxes, when I select option 5 in first drop down then another drop down box should be disabled. 有两个下拉框,当我在第一个下拉框中选择选项5时,应禁用另一个下拉框。 While, drop down values are fetched from database. 同时,从数据库中获取下拉值。

 <tr> <td class="outside scrollable-menu" style="border-style: none; border-bottom-style: none;"> Education<span style="color: red;">*</span> <form:select path="educationBean.educationId" id="education" class="form-control modalEdit"> <form:option hidden="hidden" value="">Education</form:option> <c:forEach items="${educationList}" var="education"> <form:option value="${education.educationId}">${education.educationName} </form:option> </c:forEach> </form:select> </td> <td class="outside scrollable-menu" style="border-style: none; border-bottom-style: none;"> Degree<span style="color: red;">*</span> <form:select path="degreeBean.degreeId" id="degree" class="form-control modalEdit"> <form:option hidden="hidden" value="">Degree</form:option> <c:forEach items="${degreeList}" var="degree"> <form:option value="${degree.degreeId}">${degree.degreeName} </form:option> </c:forEach> </form:select> </td> </tr> <script> function disableDegree() { if (document.getElementById("education").value === "5") { document.getElementById("deg").disabled = "true"; } else { document.getElementById("deg").disabled = "false"; } } </script> 

I have added change handler for dropdown1, and in the handler check for the selected value and based on that disable the other dropdown. 我为dropdown1添加了更改处理程序,并在处理程序中检查所选值并基于该值禁用其他下拉菜单。

Please see below runnable code snippet. 请参见下面的可运行代码段。 I hope this is what you are expecting. 我希望这就是您的期望。

 $('#Dropdown1').on('change', function() { if($(this).val()== "5") { $('#Dropdown2').prop("disabled", true); } else { $('#Dropdown2').prop("disabled", false); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <select id="Dropdown1"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <select id="Dropdown2"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="mercedes">Mercedes</option> <option value="audi">Audi</option> </select> 

you should try this 你应该试试这个

HTML HTML

<select id="ddl1" onchange='fnChange()'>
  <option value="one">One</option>
  <option value="two">Two</option>
  <option value="three">Three</option>
  <option value="Four">Four</option>
  <option value="five">Five</option>
</select>

<select id="ddl2">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>

JQuery JQuery的

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
function fnChange(){

    if($('#ddl1').val()== "Four") {
        $('#ddl2').prop("disabled", true);
    } else {
      $('#ddl2').prop("disabled", false);
    }

}

</script>

i hope this will work 我希望这会工作

we can disabled the dropdown box in following manner : Example - In html - 我们可以通过以下方式禁用下拉框:示例-在html中-

<select id="mySelect">
   <option>Apple</option>
   <option>Pear</option>
   <option>Banana</option>
   <option>Orange</option>
</select>

now in js code write : 现在在js代码中写:

function disable(){
document.getElementById("mySelect").disabled = true;

} }

run this function whenever required. 必要时运行此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM