简体   繁体   English

如何使用下拉列表选项(Javascript,HTML)默认禁用下拉列表

[英]How to disable dropdown list on default using dropdown list option (Javascript,HTML)

Greeting guys,问候伙计们,

I'm new to this coding and requires help.我是这种编码的新手,需要帮助。

I want to make dropdown list B disable on default.我想让下拉列表 B 默认禁用。

For example, in dropdown list A has "Yes", "No".例如,在下拉列表 A 中有“是”、“否”。 If user select "Yes".如果用户选择“是”。 Then the dropdown list B will be available to select.然后下拉列表 B 将可供选择。 But default, the dropdown list A is always selected = "No".但默认情况下,下拉列表 A 始终被选中 =“否”。

Dropdown list B (Disable on default)下拉列表 B(默认禁用)

<label for="new">New staff join Date: </label>
              <form method="post" action="?">
              <select name="ckb" id="ck1">
              <option disabled selected="select">-----Select Cut-----</option>
              <option value="1">January</option>
              <option value="2">February</option>
              <option value="3">March</option>

Really appreciates the help :D非常感谢您的帮助:D

Display of dropdown B should be none like below:下拉列表 B 的Display none如下所示:

<select id="B" style="display: none;">

And you should define a change event for dropdown A and in this event should check selected value of dropdown A ;并且您应该为dropdown A定义一个更改事件,并且在此事件中应该检查dropdown A选定值; if it is Yes remove display: none from dropdown B 's style else add display: none .如果是Yesdropdown B的样式中删除display: none ,否则添加display: none

You can check the value of dropdown A with JavaScript and add the disabled attribute to the dropdown B based on the value of the A.您可以使用 JavaScript 检查下拉列表 A 的值,并根据 A 的值将disabled属性添加到下拉列表 B。

This Stackoverflow thread shows a way on how to select a value from the dropdown list. 这个 Stackoverflow 线程展示了如何从下拉列表中选择一个值的方法。

After you have selected the value, you can check it's value with a simple if-else statement and add an attribute with dropdownB.setAttribute('disabled') to dropdown B.选择值后,您可以使用简单的 if-else 语句检查它的值,并将带有dropdownB.setAttribute('disabled')的属性添加到下拉列表 B。

You could use:你可以使用:

document.getElementById("elementId").disabled = true;

Just change the value to true / false to disable and enable the element.只需将值更改为true / false即可禁用和启用该元素。
And for your dropdown on Yes/No you need to add onChange eventListener to your select and in the callback function add the code to disable when selected option is Yes.对于是/否的下拉菜单,您需要将 onChange eventListener 添加到您的选择中,并在回调函数中添加代码以在选定选项为是时禁用。
For the default selected option in the YesNo select, just add selected attribute to the option you want to be selected on default.对于 YesNo 选择中的默认选择选项,只需将selected属性添加到您希望默认选择的选项。

 function onChangeYesNo(option) { if(option.value == "Yes"){ document.getElementById("dropwdown1").disabled = false; }else{ document.getElementById("dropwdown1").disabled = true; } }
 <select id="yesNo" onchange="onChangeYesNo(this)"> <option value="Yes">Yes</option> <option value="No" selected>No</option> </select> <select id="dropwdown1" disabled="true"> <option value="val1">val1</option> <option value="val2">val2</option> </select>

Here is one way to do it:这是一种方法:

 const one=document.getElementById("yesno"), two=document.getElementById("ck1"); one.onchange=()=>two.style.display=one.value=="Yes"?"":"none"
 <label for="new">New staff join Date: </label> <form method="post" action="?"> <select id="yesno"> <option>No</option> <option>Yes</option> </select> <select name="ckb" id="ck1" style="display:none"> <option disabled selected="select">-----Select Cut-----</option> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> </select> </form>

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

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